Android:在ListView的onItemClickListener()中获取TextView

发布于 2024-10-06 10:30:13 字数 574 浏览 4 评论 0原文

我正在开发 SoundBoard 应用程序。我的类扩展了 ListActivity ,列表视图如下所示

[Image] [Text]
[Image] [Text]
[Image] [Text]

现在,当用户单击任何文本视图时,我需要更改该文本视图的图像。我通过以下代码实现它。

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

TextView tv1 = (TextView) view;
//Here I change the image with tv1 as reference
}

但我需要一种替代方法来获取选定的 TextView 。因为当我使用上述方法更改图像时,如果用户选择列表中的第三行,则第二个滚动页面的第三行、第三个滚动页面的第三行等中的图像会发生变化。不仅是所需的文本视图图像发生变化,但滚动列表时即将到来的滚动页面中的一个文本视图会发生变化。希望你能明白我的问题。任何帮助表示赞赏。谢谢

I am developing a SoundBoard application. My class extends ListActivity and the listview is as follows

[Image] [Text]
[Image] [Text]
[Image] [Text]

Now when the user clicks on any textview, I need to change the image of that textview. I implement that by the following code.

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

TextView tv1 = (TextView) view;
//Here I change the image with tv1 as reference
}

But I need an alternate way to get the selected TextView . Because when I change the image using the above method, if user selects third row in the list, the image changes in the third row of the second scroll page, the third row of the third scroll page and so on gets affected.Not only the required textview image changes, but one textview in upcoming scroll pages are changed when list is scrolled. Hope you get my problem. Any help is appreciated. Thanks

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

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

发布评论

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

评论(2

温柔女人霸气范 2024-10-13 10:30:13

我认为您所看到的行为来自 Android 回收 ListView 中使用的 TextView 的事实;这就是为什么 Adapter.getView() 有一个 convertView 参数。 (一旦 TextView 滚动离开屏幕,就可以通过 convertView 参数将其传递到 Adapter.getView() 中,以重新用于已滚动的项目到显示屏上。)

您可以尝试添加 android.widget.AbsListView.OnScrollListener 到 ListView,以跟踪列表中的哪些项目在给定时间出现在屏幕上。您可能还需要重写 Adapter.getView() 来强制图像恢复为默认值(或者更一般地说,使用适合新显示项目的图像。)

I think the behavior that you're seeing comes from the fact that Android recycles the TextViews used in the ListView; that's why Adapter.getView() has a convertView argument. (Once a TextView has scrolled off the screen, it can be passed into Adapter.getView() via the convertView argument, to be re-used for an item that's been scrolled onto the display.)

You might try adding a android.widget.AbsListView.OnScrollListener to the ListView, to track which items in the list are on screen at a given time. You probably also need to override Adapter.getView() to force the image back to the default (or more generally, to use whichever image is appropriate for the newly displayed item.)

只有影子陪我不离不弃 2024-10-13 10:30:13

我没有测试我自己,您可以通过在 Textview 中添加 onClck 属性来尝试

干杯,
刈谷酱

I didn't tested my self you may try for by adding onClck attribute in Textview
<android :onclick="name of function>

Cheers,
Kariyachan

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