gpt4 book ai didi

android - 在 onListItemClick 中单独显示 HASHMAP 值

转载 作者:行者123 更新时间:2023-11-30 02:18:04 25 4
gpt4 key购买 nike

我正在使用 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;
}

}

我想要的是,每当我单击一个列表项时,键值都应该显示在单独的 toasts 中。目前我在单个 toasts 中显示两者,但不知道如何将它们分开。

最佳答案

当您点击一行时,onListItemClick 被触发。您可以使用 getItemAtPosition 检索您点击的元素:

HashMap<String, String> item = (HashMap<String, String>) l.getItemAtPosition(position);

并使用 get 方法从项目中检索信息

 String string = item.get("the_key_you_want_to_retrieve");

关于android - 在 onListItemClick 中单独显示 HASHMAP 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28969615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com