【发布时间】:2015-05-12 05:44:04
【问题描述】:
我正在使用 HASHMAP 在列表的单个列表项中显示两个项目,如下面的代码所示。
public class MainActivity extends ListActivity {
HashMap<String, String> item;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<Map<String, String>> list = buildData();
String[] from = { "name", "purpose" };
int[] to = { android.R.id.text1, android.R.id.text2 };
SimpleAdapter adapter = new SimpleAdapter(this, list,
android.R.layout.simple_list_item_2, from, to);
setListAdapter(adapter);
// setOnItemClickListener(selectLesson);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
// Map<String, String> nn = list.get(position);
String item1 = (String) getListAdapter().getItem(position).toString();
// item.get(name);
Toast.makeText(this, item1, Toast.LENGTH_LONG).show();
}
private ArrayList<Map<String, String>> buildData() {
ArrayList<Map<String, String>> list = new ArrayList<Map<String,String>>();
list.add(putData("Android", "Mobile"));
list.add(putData("Windows7", "Windows7"));
list.add(putData("iPhone", "iPhone"));
return list;
}
private HashMap<String, String> putData(String name, String purpose) {
item = new HashMap<String, String>();
item.put("name", name);
item.put("purpose", purpose);
return item;
}
}
我想要的是,每当我单击一个 listitem 键值时,都应该在单独的 toasts 中显示。目前我在一个 toasts 中显示两者,不知道如何将它们分开。
【问题讨论】:
标签: android listview arraylist hashmap