HashMap到ListView

我有HashMap,我怎么能把它放在ListView中? 哪个适配器需要使用?

public void showCinemas(HashMap cinemas) { ...//What? list.setAdapter(adapter); } 

Solutions Collecting From Web of "HashMap到ListView"

制作简单的适配器类:

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 map) { mData = new ArrayList(); mData.addAll(map.entrySet()); } @Override public int getCount() { return mData.size(); } @Override public Map.Entry 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 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; } } 

布局/ my_adapter_item.xml

 < ?xml version="1.0" encoding="utf-8"?>     

你的代码:

 public void showCinemas(HashMap cinemas) { MyAdapter adapter = new MyAdapter(cinemas); list.setAdapter(adapter); } 

HashMap由2个Collection(或更好的1个Collection和1个Set)组成,所以扩展ArrayAdapter是不可能的。 但是你可以轻松地获得Map.Entry的Collection(或更好的Set),并将其转换为List:

从:

 Map map = new HashMap(); 

至:

 List> list = new ArrayList(map.entrySet()); 

所以我使用像这样的派生ArrayAdapter:

 class HashMapArrayAdapter extends ArrayAdapter { private static class ViewHolder { TextView tV1; TextView tV2; } public HashMapArrayAdapter(Context context, int textViewResourceId, List> 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 entry = (Map.Entry) this.getItem(position); viewHolder.tV1.setText(entry.getKey()); viewHolder.tV2.setText(entry.getValue().toString()); return convertView; } 

然后创建适配器:

  ArrayAdapter adapter = new HashMapArrayAdapter(this.getActivity(), android.R.layout.simple_list_item_2, new ArrayList(map.entrySet())); 

它很简单:

对于您要创建的列表项,例如。 假设您必须将学生的记录放在名称和地址等列表项中

 private HashMap prepareListViewItems(Student[] student) { ArrayList> listdata = new ArrayList>(); for(int index=0;index data = new HashMap(); data.put("roll", student[indxe].roll); data.put("address", student[indxe].address); data=null; listdata.add(data); } return data; } private void setListAdapter(Student[] students) { TestListAdapter adapter = new TestListAdapter(prepareListViewItems(students)) list.setAdapter(adapter); } 

但是,当您创建自定义适配器时,不需要创建hashmap,只有数组才足以满足您的需要。