Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HashMap to ListView

Tags:

I have HashMap, how can I to put it in ListView? Which adapter need to use?

    public void showCinemas(HashMap<String, String> cinemas) {     ...//What?     list.setAdapter(adapter); } 
like image 366
Suleiman Avatar asked Oct 19 '13 13:10

Suleiman


2 Answers

Make simple adapter class:

MyAdapter.java

import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView;  import java.util.ArrayList; import java.util.Map;  public class MyAdapter extends BaseAdapter {     private final ArrayList mData;      public MyAdapter(Map<String, String> map) {         mData = new ArrayList();         mData.addAll(map.entrySet());     }      @Override     public int getCount() {         return mData.size();     }      @Override     public Map.Entry<String, String> getItem(int position) {         return (Map.Entry) mData.get(position);     }      @Override     public long getItemId(int position) {         // TODO implement you own logic with ID         return 0;     }      @Override     public View getView(int position, View convertView, ViewGroup parent) {         final View result;          if (convertView == null) {             result = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_adapter_item, parent, false);         } else {             result = convertView;         }          Map.Entry<String, String> item = getItem(position);          // TODO replace findViewById by ViewHolder         ((TextView) result.findViewById(android.R.id.text1)).setText(item.getKey());         ((TextView) result.findViewById(android.R.id.text2)).setText(item.getValue());          return result;     } } 

layout/my_adapter_item.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout         xmlns:android="http://schemas.android.com/apk/res/android"         android:orientation="vertical"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         >      <TextView             android:id="@android:id/text1"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             />      <TextView             android:id="@android:id/text2"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             /> </LinearLayout> 

Your code:

public void showCinemas(HashMap<String, String> cinemas) {     MyAdapter adapter = new MyAdapter(cinemas);     list.setAdapter(adapter); } 
like image 200
Oleksii K. Avatar answered Oct 09 '22 01:10

Oleksii K.


HashMap is made of 2 Collection (or better 1 Collection and 1 Set), so it is not really possible by extending ArrayAdapter; but you can easily get a Collection (or better a Set) of Map.Entry, and convert it to a List:

From:

Map<String, Object> map = new HashMap<String, Object>(); 

to:

List<Map.Entry<String, Object>> list = new ArrayList(map.entrySet()); 

So I use a derived ArrayAdapter like this one:

class HashMapArrayAdapter extends ArrayAdapter {          private static class ViewHolder {             TextView tV1;             TextView tV2;         }          public HashMapArrayAdapter(Context context, int textViewResourceId, List<Map.Entry<String, Object>> objects) {             super(context, textViewResourceId, objects);         }          @Override         public View getView(int position, View convertView, ViewGroup parent) {              ViewHolder viewHolder;              if (convertView == null) {                 convertView = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_2, parent, false);                 viewHolder = new ViewHolder();                 viewHolder.tV1 = (TextView) convertView.findViewById(android.R.id.text1);                 viewHolder.tV2 = (TextView) convertView.findViewById(android.R.id.text2);                 convertView.setTag(viewHolder);             } else                 viewHolder = (ViewHolder) convertView.getTag();              Map.Entry<String, Object> entry = (Map.Entry<String, Object>) this.getItem(position);              viewHolder.tV1.setText(entry.getKey());             viewHolder.tV2.setText(entry.getValue().toString());             return convertView;         } 

And then to create the adapter:

 ArrayAdapter adapter = new HashMapArrayAdapter(this.getActivity(), android.R.layout.simple_list_item_2, new ArrayList(map.entrySet())); 
like image 31
Zanna Avatar answered Oct 09 '22 01:10

Zanna