开发者

Android: Get the TextView in onItemClickListener() of ListView

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<?> pa开发者_如何学编程rent, 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


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.)


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

Cheers, Kariyachan

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜