Why does this give me an error in the below fragment?
This is my code:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, spinnerArray);
Try this:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_spinner_item, spinnerArray);
getContext() was added only on API 23 and should work only with few devices (since Android M still does not have a huge market share).
getActivity() was added on API 11 and it is NOT deprecated.. So, you can use it.
Replace this with this.getContext() or even simply just getContext()
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),
android.R.layout.simple_spinner_item, spinnerArray);
The reason being is that the constructor for an ArrayAdapter object looks like this,
From https://developer.android.com/reference/android/widget/ArrayAdapter.html,
ArrayAdapter(Context context, int resource, T[] objects)
or
ArrayAdapter(Context context, int resource, List<T> objects)
What was probably happening before is that the class that the line of code was in was most likely not of type Context; therefore, you can't use this alone as a parameter in the construction of an ArrayAdapter. If getContext() doesn't work, then I'd suggest trying getActivity() or getApplicationContext() as well.
Use getActivity() which return the context of the activity that the fragment is associated with.
So your code becomes
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, spinnerArray);`]
You can try getActivity().getApplicationContext() instead of using just this in fragment.
Related
How can we use livedata in spinners? I have the following:
ArrayAdapter<CharSequence> adapter;
LiveData<List<Site>> sites = SiteDatabase.getInstance(this).siteDao().getAllSites();
Spinner spinner = findViewById(R.id.spinnerSites);
adapter = new ArrayAdapter<Site>(this,
this,android.R.layout.simple_spinner_dropdown_item, sites); //What Goes Here ? This gives Cannot Resolve Constructor Error...
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Thanks!
You need to replace this line
adapter = new ArrayAdapter<Site>(this,android.R.layout.simple_dropdown_item_1line, sites);
with this
adapter = new ArrayAdapter<Site(this,android.R.layout.simple_dropdown_item_1line, sites.getValue());
The signature for your ArrayAdapter is like this ArrayAdapter(Context context, int resource, List<Site> objects). You are correctly passing the Context and int param however the last param needs to be a List<Site> instead of a LiveData<List<Site>.
i'm doing a system of news feed, so, i've a difficulty for adapter my ArrayAdapter insert value into ListView, with: "username, name, picture, content and others values" i'm newbie in Android Studio.
Searching the internet I found this function
HashMap<String,String> persons = new HashMap<String,String>();
persons.put(TAG_ID,id);
persons.put(TAG_NAME,name);
persons.put(TAG_ADD,address);
personList.add(persons);
}
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, personList, R.layout.list_item,
new String[]{TAG_ID,TAG_NAME,TAG_ADD},
new int[]{R.id.id, R.id.name, R.id.address}
);
list.setAdapter(adapter);
But, i use ArrayList and ArrayAdapter:
ArrayList<Post> postList =
new JsonConverter<Post>().toArrayList(s, Post.class);
ArrayList<String> postador = new ArrayList<String>();
for(Post value: postList){
postador.add(value.id_postador);
postador.add(value.post_legenda);
if(value.post_foto != null) {
postador.add(value.post_foto);
}
postador.add(value.post_data);
postador.add(value.curtidas);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this,
android.R.layout.simple_list_item_1, postador
);
lvPost.setAdapter(adapter);
I tried it, but no success.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
MainActivity.this,
R.layout.list_item, postador, new String[]{TAG_ID,TAG_NAME,TAG_ADD},
new int[]{R.id.id, R.id.name, R.id.address}
);
There a method convert ListAdapter in ArrayAdapter or adapter my ArrayAdapter for ListAdapter?
If you are a newbie to Android, I would like to recommend few things to you.
1). Start Using RecyclerView from the very beginning instead of ListView. It is more advanced, flexible and efficient version of ListView. You can find a sample demo for the RecyclerView here and here
2). If you still want to use ListView. Then you further have two options, either to use BaseAdapter or ArrayAdapter.
a). Base Adapter as the name suggests, is a base class for all the adapters. If you need more flexibility, you can go with it. You can find the demo sample here and here
b). On the other hand, ArrayAdapter is a concrete BaseAdapter that is backed by an array of arbitrary objects. By default this class expects that the provided resource id references a single TextView.If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource.To use something other than TextViews (like in your case) for the array display, for instance, ImageViews, or to have some of data besides toString() results fill the views, override getView(int, View, ViewGroup) to return the type of view you want. You can find the demo sample here and here.
Hope it will help you in future and also I suppose you will get answer to your question in the specified demo samples. Thanks and have a good day.. :)
I keep getting this error http://prntscr.com/3qygj9 (Sorry, I couldnt copy and paste) from this segment of code
final ArrayList<HashMap<String, String>> foodList = db.getAllFood();
if(foodList.size() != 0){
ListView listView = (ListView) findViewById(R.id.foodListView);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
foodList );
listView.setAdapter(arrayAdapter);
}
I can't for the life of me figure out why. Thanks in advance!
please Use ArrayAdapter<HashMap<String, String>> instead of ArrayAdapter<String>
ArrayAdapter<HashMap<String, String>> arrayAdapter = new ArrayAdapter<HashMap<String, String>>(
this,
android.R.layout.simple_list_item_1,
foodList );
The type you're using to build the ArrayAdapter does not look compatible at all.
To build an ArrayAdapter<String>, the last argument to the constructor should be of type ArrayList<String>. But in your code, the foodList variable you pass for that argument is of type ArrayList<HashMap<String, String>>.
protected void onPostExecute(String s) {
veriAdaptoru=new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, android.R.id.text1, a);
liste=(ListView)findViewById(R.id.listView1);
liste.setAdapter(veriAdaptoru);
}
i have to write this code but in this part i get error
(veriAdaptoru=new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, android.R.id.text1, a);)
The problem is in new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, android.R.id.text1, a);
you must load context from Activity, but if you write this in onPostExecute it is another. Make in you Activity class some variable
Context context = MyActivity.this;
if your AsyncTask is in another class - create constructor and pass context from Activiy in it.
and then you can do
(context, android.R.layout.simple_list_item_1, android.R.id.text1, a);`
I am trying to add a two-column ListView to my android app. When I created the project, I selected the option Fragment which creates that beautiful navigation by sliding to left and right. So my MainActivity extends FragmentActivity.
my problem is when I am trying to add AddayAdapter to ListView, the construstor of ArrayAdapter looks for a context object and I dont know to pass that.
here is the code where I get error
StableArrayAdapter adapter = new StableArrayAdapter(this, R.layout.row , list);
listview.setAdapter(adapter);
When I pass "this", it passes FragmentActivity because the activity extends FragmentActivity. but constructor need a "Context" object.
it gives this error message
"The constructor MainActivity.StableArrayAdapter(MainActivity.DummySectionFragment, int, ArrayList<String>) is undefined"
How can I pass Context object?
You are setting the adapter inside a fragment. Use
StableArrayAdapter adapter = new StableArrayAdapter(this.getActivity(), R.layout.row , list);
StableArrayAdapter adapter = new StableArrayAdapter(getContext(), R.layout.row , list);
listview.setAdapter(adapter);
Did you try passing something like this from your Fragment. Try either getContext() or getApplicationContext().