Android: Handle ListView Recycle

不问归期 提交于 2019-12-24 03:17:02

问题


I am developing a soundboard application in which I use Listview Activity. But since Listview of Android has the property of recycling its listviews, the changes I make to the selected textview gets reflected in all the pages while scrolling the listview. I don't want this to happen. So how do I handle it properly. If somebody can help me out with code snippet, it will be really helpful and I appreciate your effort. Thank you!

Edit: Hope this will explain better

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 tv = (TextView) view;
//Here I change the image with tv as reference
final Resources res = getBaseContext().getResources();
    final Drawable myImage = res.getDrawable(R.drawable.pause);
  //  myImage.
    final Drawable myImage1 = res.getDrawable(R.drawable.play);

    tv.setCompoundDrawablesWithIntrinsicBounds(myImage, null, null, null);

    MediaPlayer.OnCompletionListener listener = new MediaPlayer.OnCompletionListener(){
      public void onCompletion(MediaPlayer mp) {

         tv.setCompoundDrawablesWithIntrinsicBounds(myImage1, null, null, null);


      }
    };
}

回答1:


Short answer: Have your list's adapter handle updates to the data to render the changes correctly. That is, override/edit the getView() method if necessary to know how to handle the data change, edit the data underlying your Adapter, call notifyDataSetChanged(), and let those data changes propagate down to views when getView gets called. Right now, you're probably hand-modifying the views without changing the underlying data, which is a violation of MVC patterns regardless of the view-recycling bits.

Given how generically you asked the question, though, it'd be rather difficult to give you a code sample that fits your particular case.



来源:https://stackoverflow.com/questions/4395824/android-handle-listview-recycle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!