wufoo
wufoo

Reputation: 14571

Inflate ListView row from OnClickListener in Android?

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

Upvotes: 0

Views: 2086

Answers (2)

Pragnani
Pragnani

Reputation: 20155

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

Upvotes: 2

Raghunandan
Raghunandan

Reputation: 133560

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.

Upvotes: 1

Related Questions