我有 HashMap,如何把它放在 ListView 中?需要使用哪个适配器?
public void showCinemas(HashMap<String, String> cinemas)
{
...//What?
list.setAdapter(adapter);
}
制作简单的适配器类:
我的适配器.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;
}
}
布局/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>
你的代码:
public void showCinemas(HashMap<String, String> cinemas) {
MyAdapter adapter = new MyAdapter(cinemas);
list.setAdapter(adapter);
}
HashMap 由 2 个 Collection 组成(或者更好的是 1 个 Collection 和 1 个 Set),因此通过扩展 ArrayAdapter 是不可能的;但是您可以轻松获得 Map.Entry 的 Collection(或更好的 Set),并将其转换为 List:
从:
Map<String, Object> map = new HashMap<String, Object>();
到:
List<Map.Entry<String, Object>> 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<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;
}
然后创建适配器:
ArrayAdapter adapter = new HashMapArrayAdapter(this.getActivity(), android.R.layout.simple_list_item_2, new ArrayList(map.entrySet()));
这与上面 Zanna 的答案有一些相似之处,但更清洁、改进和更全面。我认为这很简单。
适配器:
public class MapEntryListAdapter extends ArrayAdapter<Map.Entry<String, Object>>
{
public MapEntryListAdapter (Context context, List<Map.Entry<String, Object>> entryList)
{
super (context, android.R.layout.simple_list_item_2, entryList);
}
@NonNull @Override
public View getView (int position, View convertView, ViewGroup parent)
{
View currentItemView = convertView != null ? convertView :
LayoutInflater.from (getContext ()).inflate (
android.R.layout.simple_list_item_2, parent, false);
Map.Entry<String, Object> currentEntry = this.getItem (position);
TextView textViewKey = currentItemView.findViewById (android.R.id.text1);
TextView textViewValue = currentItemView.findViewById (android.R.id.text2);
textViewKey.setText (currentEntry.getKey ());
textViewValue.setText (currentEntry.getValue ().toString ());
return currentItemView;
}
}
MainActivity - 字段:
private Map<String, Object> mMapItems; // original data source of all items
private List<Map.Entry<String, Object>> mListOfMapEntries; // list of entries from mMapItems
private MapEntryListAdapter mMapEntryListAdapter;
MainActivity - onCreate 方法:(相关部分)
mMapItems = new LinkedHashMap<> ();
mMapItems.put ("Test Key", "Test Value"); // put in sample item #1
mMapItems.put ("Sample Key", "Sample Value"); // put in sample item #2
mListOfMapEntries = new ArrayList<> (mMapItems.entrySet ()); // create the list
mMapEntryListAdapter = new MapEntryListAdapter (this, mListOfMapEntries);
ListView listView = findViewById (R.id.list_view);
listView.setAdapter (mMapEntryListAdapter);
注意:根据设计,此适配器的数据源不会复制传入的 entryList,而是指向同一个实际列表。这使您能够轻松地修改 MainActivity 中的 Adapter 数据,或者在您引用此对象的 Adapter 的任何其他地方。然后,您需要调用适配器的 notifyDataSetChanged() 方法,让它知道列表已更改,以便更新 ListView 以反映这些更改。
如果您的 Map 对象的内容稍后发生更改,您可以执行以下操作以将这些更新带入您的列表,然后再带入您的 ListView:
mListOfMapEntries.clear ();
mListOfMapEntries.addAll (mMapItems.entrySet ());
mMapEntryListAdapter.notifyDataSetChanged ();
这会清除所有现有项目的列表,然后将 Map 中的项目添加到列表中,然后,非常重要的是,告诉适配器更新 ListView。
(注意:不要在此处创建新的 List 对象(与此处显示的清除和添加相反),因为这样您将不再修改仍指向原始列表的适配器的数据源。)
它很简单:
对于您要创建的列表项,例如。假设您必须将学生的记录放在姓名和地址等列表项中
private HashMap<String,Object> prepareListViewItems(Student[] student)
{
ArrayList<HashMap<String,Object>> listdata = new ArrayList<HashMap<String,Object>>();
for(int index=0;index<student.size();index++)
{
HashMap<String,Object> data = new HashMap<String,Object>();
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,只需数组即可满足您的目的。