Listview获取项目文本

时间:2010-12-22 20:43:32

标签: android listviewitem

我需要抓取所选项目TEXT(不是id)。 我可以在onListItemClick下编写什么来做到这一点? 我明白了:

java.lang.ClassCastException: org.eclipse.jdt.internal.debug.core.model.JDIObjectValue cannot be cast to org.eclipse.jdt.debug.core.IJavaArray
at org.eclipse.jdt.internal.debug.ui.JavaDetailFormattersManager.getCompiledExpression(JavaDetailFormattersManager.java:396)
at org.eclipse.jdt.internal.debug.ui.JavaDetailFormattersManager.resolveFormatter(JavaDetailFormattersManager.java:167)
at org.eclipse.jdt.internal.debug.ui.JavaDetailFormattersManager.access$1(JavaDetailFormattersManager.java:152)
at org.eclipse.jdt.internal.debug.ui.JavaDetailFormattersManager$3.run(JavaDetailFormattersManager.java:143)
at org.eclipse.jdt.internal.debug.core.model.JDIThread$ThreadJob.run(JDIThread.java:2751)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)

2 个答案:

答案 0 :(得分:2)

传递给onItemClick的一件事是被点击的视图:

  

abstract void onItemClick(AdapterView parent,View view,int position,long id)

view投射到相应的类型并在其上调用getText();例如:

final String text = ((TextView)view).getText();

答案 1 :(得分:-1)

有多种方法可以实现这一点,这也取决于您是从简单的listView获取文本还是从Custom ListView(使用custom_list_item.xml)获取文本。

对于Simple ListView

lv.setOnItemClickListener(new OnItemClickListener(){

    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
        String text = lv.get(position).tostring().trim();//first method 
        final String text = ((TextView)view).getText();// second method
}});

对于自定义列表视图

lv.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
        TextView textView = (TextView) view.findViewById(R.id.list_content);
//where list_content is the id of TextView in listview_item.xml

}});