0

I have a class (A_Main.java) extending ArrayAdapter. I set my ListView to use A_Main as it's ListAdapter. Inside A_Main.getView() I inflate the view to get at the ListView widgets for each row. Each row contains a TextView, CheckBox and an ImageButton. When the ImageButton is clicked, I play the song associated with the TextView. I don't want to use onItemClickListener() on the ListView as it's too easy to fumble up a scroll and start playing a new song.

When I click an ImageButton in a new row, I need to un-hilite the ImageButton of the currently playing song, and hilite the new one. I'm thinking the way to do that would be to inflate the view in the ImageButton's onClickListener() and un-hilite every button in the List, then, hi-lite the one which is playing. I'm not sure the best way to go about this. Can I keep a member list in A_Main of each ImageButton ID as getView() iterates over them and reference the ID directly from onClickListener() without causing memory leaks? Do those IDs disappear as soon as getView() is done with them? Any thoughts on alternative approaches?

enter image description here

wufoo
  • 13,571
  • 12
  • 53
  • 78

2 Answers2

2

Edit:

Solution is probably simple Take boolean array globally like this

 private final boolean[] selectedstates;

And initialize it with the size of your list in your constructor

 selectedstates= new boolean[yourlist.size()];

And in out side of onclick listener set like this

 yourbutton.setSelected(selectedstates[position]);

I hope this will help you


Try this

Take a custom selector with two different state images for selection and non selection

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/pause_button"
          android:state_selected="true" />
    <item android:drawable="@drawable/play_button" />
</selector>

1.Create a global variable

Imageview previous; 

in your Custom Adapter and Initialize it in the constructor where you'll get the content

previous=new ImageView(context);

Add in your adapter getView() method you will probably have a onclickListener for your Imageview do like this

 imagPlay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             ImageView current=((ImageView)v);
              current.setSelected(true);
              previous.setSelected(false);
              previous=current;
        }
    });

This will work, I was confident because I have used it in my app. I hope this will help you

Pragnani
  • 20,075
  • 6
  • 49
  • 74
  • After some testing, the ListView isn't updating button states correctly when scrolled. Some buttons appear selected which haven't been. What are you using in getView() to correct button state? – wufoo Mar 06 '13 at 18:46
  • Hey sorry for the late reply, it was night in India so I went to sleep yesterday...I'll update answer.. – Pragnani Mar 07 '13 at 04:48
  • 1
    No problem. Funny! I ended up implementing the exact same thing you suggested. Thanks for following-up. – wufoo Mar 07 '13 at 17:48
1

Should look at this video. http://www.youtube.com/watch?v=wDBM6wVEO70. Especially the viewholder part to reuse views and avoid memory leaks. To highlight a listview row button check the position of the item on which you click and highlight the button by setting a background for the button.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Thanks, great video! I've implemented the ViewHolder class it talks about. I always suspected the constant findViewById() calls were a performance hit but never knew what to do about . – wufoo Mar 06 '13 at 18:32