Listview获取项目文本

发布于 2024-10-08 13:39:30 字数 851 浏览 5 评论 0原文

我需要获取所选项目的文本(不是 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)

I need to grab selected item TEXT (not id).
What can I write under onListItemClick to do that?
I get:

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绅士风度i 2024-10-15 13:39:30

传递给 onItemClick 的内容之一是被单击的视图:

abstract void onItemClick(AdapterView 父级,View 视图,int 位置,长 id)

view 转换为适当的类型并对其调用 getText() ;例如:

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

One of the things passed to onItemClick is the view that was clicked:

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

Cast view to the appropriate type and call getText() on it; for example:

final String text = ((TextView)view).getText();
完美的未来在梦里 2024-10-15 13:39:30

有多种方法可以实现此目的,这还取决于您是从简单的 listView 还是从自定义 ListView(使用 custom_list_item.xml)获取文本。

对于简单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
}});

对于自定义ListView

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

}});

There are multiple ways to achieve this and it also depends on whether you are getting text from simple listView or from Custom ListView(with custom_list_item.xml).

For 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
}});

For Custom ListView

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

}});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文