Push ListView string from OnClickListener in Android?

I have a class ( A_Main.java ) extending the ArrayAdapter . I set ListView to use A_Main as a ListAdapter . Inside A_Main.getView() I inflate the view to get ListView widgets for each row. Each row contains a TextView , CheckBox and ImageButton . When I click the ImageButton button ImageButton I play the song associated with the TextView . I do not want to use onItemClickListener() on the ListView , since it is too easy to scroll through the scrolling and start playing a new song.

When I click ImageButton in a new line, I need to remove the hilite ImageButton current song and hilite to a new one. I think that for this it would be necessary to inflate the view in ImageButton onClickListener() and non-hilite each button in the list, and then the hi-lite is the one that plays. I am not sure if this is the best way. Can I save the list of participants in A_Main each A_Main identifier when getView () iterates over them and references the identifier directly from onClickListener() without causing a memory leak? Are these identifiers getView() as soon as getView() is executed with them? Any thoughts on alternative approaches?

enter image description here

0
source share
2 answers

Edit:

The solution is probably simple. Take a 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 aside from listening to onclick as shown

  yourbutton.setSelected(selectedstates[position]); 

I hope this helps you


try it

Take a custom selector with two different state images for selection and without 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 user adapter and initialize it in the constructor where you get the content

 previous=new ImageView(context); 

Add to your getView () method you'll probably have an onclickListener for your Imageview do 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 sure because I used it in my application. Hope this helps you.

+2
source

Check out this video. http://www.youtube.com/watch?v=wDBM6wVEO70 . In particular, the role of the viewer is to reuse performances and prevent memory leaks. To highlight a button on a list line, check the position of the item you click on and select the button by setting the background for the button.

+1
source

All Articles