gpt4 book ai didi

android - 如何从 ListView 适配器位置获取对象值

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:46:31 24 4
gpt4 key购买 nike

如何从适配器位置获取值,我在下面有代码:

CategoriesXmlParser categoryXmlParser = new CategoriesXmlParser();
List<HashMap<String, Object>> categories = null;

try {
categories = categoryXmlParser.parse(reader);
} catch (Exception e) {
Log.d("Exception", e.toString());
}

String[] from = { "name", "image" };
int[] to = { R.id.nama_category, R.id.logo_category };

final SimpleAdapter adapter = new SimpleAdapter(getBaseContext(),
categories, R.layout.per_item_category, from, to);

mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Object obj = mListView.getAdapter().getItem(position);
String value = obj.toString();
Log.d("MyLog", "Value is: "+value);
String name = // how code to get name value.
}
});

如果我在 MyLog 的 logcat 上查看它,我得到的是:

Value is: {position=12, image_path=http://192.168.103.121/xml/icon.png, link=http://192.168.103.121/xml/category.php?kat_id=13, name=Category 13}

所以我的问题是,我想从名称中获取值并存储到变量字符串名称中,我只想在字符串名称中获取“类别 13”。因为我想将它传递给另一个 Activity 。

最佳答案

貌似你用hashmap做了object,比如blablabla.put("name", "value")??如果是。试试这个:

mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Object obj = mListView.getAdapter().getItem(position);
String value = obj.toString();
Log.d("MyLog", "Value is: "+value);
String name = // how code to get name value.
}
});

更改为:

mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HashMap<String, Object> obj = (HashMap<String, Object>) adapter.getItem(position);
String name = (String) obj.get("name");
Log.d("Yourtag", name);
}
});

关于android - 如何从 ListView 适配器位置获取对象值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14393334/

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