2

I am using following code but it does not populates the listview.

  ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
    lv.setAdapter(new ArrayAdapter<HashMap<String, String>> (this,android.R.layout.simple_list_item_1 , menuItems));

The problem is the above code creates adapter successfully but it does not show in listview.

Thank you

2
  • arrayadapter couldnt work with hashmap array list have to be string try to create simple type arraylist hashmap.getvalues Commented Dec 1, 2012 at 15:15
  • stackoverflow.com/questions/15816590/… Refer this Commented Mar 9, 2015 at 9:32

1 Answer 1

2

You can implement SimpleAdapter for filling data from ArrayList of HashMap as:

SimpleAdapter listadapter = new SimpleAdapter(this, menuItems, R.layout.row,

new String[] {"textone", "texttwo", "txtthree"}, new int[] 

{R.id.textone, R.id.texttwo, R.id.texttwo});

but for implementing SimpleAdapter you must create a layout for row with Views you want in single row

for more help you can see following tutorials to fill ListView with ArrayList of hashMap:

http://shenhengbin.wordpress.com/2012/03/17/listview-simpleadapter/

http://shenhengbin.wordpress.com/2012/03/17/listview-simpleadapter/

Sign up to request clarification or add additional context in comments.

7 Comments

This adapter works for custom list while I am using the listview control
@user1214458 : i'm not clear what u want to say. ListView control is always same but it's delend on you either use custom Adapter or or Default Adapter getView method to create listview row
I above i am confused in new int[] {R.id.textone, R.id.texttwo, R.id.texttwo}); for listview we do not have these ..
These three are id of View to which you want to map HaspMap Values.like suppose you have Three item in one HaspMap instance and your ArrayList content 5 instances of this HashMap.you want to show these three items in one row of listView.then what you will need Create an layout for listview row with Three textView or any other Views like button ,imageView's etc. and pass these controls id to SimpleAdapter new int[] {R.id.textone, R.id.texttwo, R.id.texttwo}) as last parameter and row layout id ` R.layout.row` as third parameter
and i also share an tutorial link in my answer in which author is doing same as you want
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.