如何让ListView中选定行中的文本,如果ListView控件得到了项目,子项目 [英] How to get text in a selected row in ListView, if ListView got item and subitem

查看:206
本文介绍了如何让ListView中选定行中的文本,如果ListView控件得到了项目,子项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView

I have a listview

ListView poiList = (ListView) findViewById(R.id.poiList);

和IM与填充它。

SimpleAdapter adapter = new SimpleAdapter(this, data,
                        android.R.layout.simple_list_item_2,
                        new String[] {"name", "dist"},
                        new int[] {android.R.id.text1,
                                   android.R.id.text2});
        poiList.setAdapter(adapter);

通过这个SimpleAdapter,有一个项目和在ListView各行的子项。一切完美的作品。

With this SimpleAdapter, there's an item and a subitem for each row in the ListView. Everything works perfectly.

现在我实现onItemClick为我的ListView:

Now I implement onItemClick for my ListView:

poiList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {

            String poiName = ((TextView)view).getText().toString();
}
 }

但我不能让我选择的行中的文本。我认为有超过该行(项目子项和)在一个单一的TextView。我怎么可以参考呢?

But I cant get the text in the row that I selected. I think There's more than a single TextView in that row ( Item and subitem). How can I refer to them?

推荐答案

由于我评论的时候,你张贴的问题,你应该使用:

As I commented when you posted the question, you should use:

((TextView)(view.findViewById(android.R.id.text1))).getText().toString()

因为查看不是的TextView 本身,而是列表项的查看

since the View is not the TextView itself, but the List Item's View.

高兴它解决您的问题,顺便说一句。干杯。

Glad it solved your problem, by the way. Cheers.

这篇关于如何让ListView中选定行中的文本,如果ListView控件得到了项目,子项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆