如何从listView上的选定项中提取文本 [英] How to extract the text from the selected item on the listView
本文介绍了如何从listView上的选定项中提取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含一些项目的列表视图.我想从所选项目中获取文本.
I have a listview with some items. I would like to get the text from the selected item.
这是我的列表适配器和 onItemClickListener:
Here is my list adapter and the onItemClickListener:
ListView lv = (ListView)findViewById(R.id.listView1);
lv.setAdapter(new ArrayAdapter<Country>(
this,R.layout.list_black_text,R.id.list_content, values));
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
??????
}});
}
你能告诉我如何从所选项目中获取字符串.
Could you tell me please how to get the String from the selected item.
方法 ((TextView) view).getText() 不起作用,我有一个
the method ((TextView) view).getText() does not work, i have a
ClassCastException: android.widget.LinearLayout
我找到了解决方案,也许有人会需要它:
I have found the solution, maybe somebody will need it:
ListView lv = (ListView)findViewById(R.id.listView1);
lv.setAdapter(new ArrayAdapter<Country>(
this,R.layout.list_black_text,R.id.list_content, values));
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView textView = (TextView) view.findViewById(R.id.list_content);
String text = textView.getText().toString();
System.out.println("Choosen Country = : " + text);
}});
推荐答案
使用这个:
String selectedFromList = (String) (lv.getItemAtPosition(position));
无论列表中的数据类型是什么,都要进行相应的转换.
Whatever the datatype you are having in your list, cast accordingly.
希望它会有所帮助.:)
Hope it will help. :)
这篇关于如何从listView上的选定项中提取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文