Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What adapter shall I use to use HashMap in a ListView

Tags:

android

I want to use HashMap for a list of items of Adapter for a ListView. I was going to use ArrayAdapter<> but I can't because it is working with List<> only. What adapter shall I use?

like image 950
Eugene Avatar asked Mar 08 '11 15:03

Eugene


People also ask

Which adapter is used to map static data into views?

Here Simple Adapter is one type of Adapter. It is basically an easy adapter to map static data to views defined in our XML file(UI component) and is used for customization of List or Grid items. Here we use an ArrayList of Map (e.g. hashmap, mutable map, etc.) for data-backing.

How do you connect an adapter with ListView write down the codes for it?

Attaching the Adapter to a ListView // Construct the data source ArrayList<User> arrayOfUsers = new ArrayList<User>(); // Create the adapter to convert the array to views UsersAdapter adapter = new UsersAdapter(this, arrayOfUsers); // Attach the adapter to a ListView ListView listView = (ListView) findViewById(R. id.

What are adapters how it is different from list controls?

Adapter is an interface whose implementations provide data and control the display of that data. ListView s own adapters that completely control the ListView 's display. So adapters control the content displayed in the list as well as how to display it.


2 Answers

There are no predefined Adapters which will render a HashMap. I suggest creating your own Adapter by extending BaseAdapter.

Edit: It is posible to use HashMap with and extended BaseAdapter, here's an(untested) example:

public class HashMapAdapter extends BaseAdapter {      private HashMap<String, String> mData = new HashMap<String, String>();     private String[] mKeys;     public HashMapAdapter(HashMap<String, String> data){         mData  = data;         mKeys = mData.keySet().toArray(new String[data.size()]);     }      @Override     public int getCount() {         return mData.size();     }      @Override     public Object getItem(int position) {         return mData.get(mKeys[position]);     }      @Override     public long getItemId(int arg0) {         return arg0;     }      @Override     public View getView(int pos, View convertView, ViewGroup parent) {         String key = mKeys[pos];         String Value = getItem(pos).toString();          //do your view stuff here          return convertView;     } } 

This comes with the following caveat, the order of the items is not guaranteed to be the same order you added them. Writing this example has made me realize; Don't use HashMap in an adapter :)

like image 53
longhairedsi Avatar answered Sep 22 '22 05:09

longhairedsi


Thanks for the answer longhairedsi

This comes with the following caveat, the order of the items is not guaranteed to be the same order you added them. Writing this example has made me realize; Don't use HashMap in an adapter :)

To get around this problem, use a LinkedHashMap instead.

public class HashMapAdapter extends BaseAdapter {     private LinkedHashMap<String, String> mData = new LinkedHashMap<String, String>();     private String[] mKeys;     public HashMapAdapter(LinkedHashMap<String, String> data){         mData  = data;         mKeys = mData.keySet().toArray(new String[data.size()]);     }      @Override     public int getCount() {         return mData.size();     }      @Override     public Object getItem(int position) {         return mData.get(mKeys[position]);     }      @Override     public long getItemId(int arg0) {         return arg0;     }      @Override     public View getView(int pos, View convertView, ViewGroup parent) {         String key = mKeys[pos];         String Value = getItem(pos).toString();          //do your view stuff here          return convertView;     } } 
like image 23
Gorba Avatar answered Sep 25 '22 05:09

Gorba