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>>.
Related
I have an issue with my ListView where for some unknown reason each element has a button that shouldn't have. How could i go about removing these buttons?
Android emulator showing the buttons in the listview
The issue seems to be with my onPostExecute method for an async task.
#Override
protected void onPostExecute(String pResult) {
//Populate the list view?
ListView listView = (ListView) findViewById(R.id.DownloadablePuzzlesList);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(),
R.layout.activity_download, R.id.DownloadablePuzzlesText, PuzzleNames);
listView.setAdapter(arrayAdapter);
}
After hours of trying to figure this issue out i'm unable to find the solution to my issue any help would be appreciated.
Your R.layout.activity_download must be having button.
Instead of:
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.activity_download, R.id.DownloadablePuzzlesText, PuzzleNames);
Do:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, android.R.id.text1, PuzzleNames)
Move your adapter initialization inside onCreate. You need to set the adapter to the ListView using setAdapter. Call noftifyDataSetChanged after populating the dataset arr.
Try this code,
private MyAdapter ma;
onCreate(){
...
setContentView(R.layout.activty_main2);
lv = (ListView) findViewById(R.id.lv_1);
ma = new MyAdapter(this, arr);
lv.setAdapter(ma);
initData();
}
initData(){
for(;;){
...
arr.add(n);
}
ma.noftifyDataSetChanged();
}
You not having any blank constructor in MyAdapter,
you have to pass context and ArrayList to the adapter like,
MyAdapter ma = new MyAdapter(Main2Activity.this,arr);
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.
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.. :)
Hey guys I am trying to create a menu using a listview with the code I have shown below. The code I have shown will show Item1, Item2 and Item3 in a list. Now I want to be able to add a spinner to item1 and item2. Is that possible? If so how would i go about doing it.
public class MyList extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] MyList = new String[] { "Item1","Item2","Item3"};
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, MyList));
}
Yes, It is possible. Try the following code. You will get it.
String[] MyList = new String[] { "Item1","Item2","Item3"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, MyList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
I think you need to create custom list view for doing that you can start by following this particular tutorial :
Tutorial
This link may be helpful to you :
Link