android - Finding the correct child view of ListView while clicking a button within it -



android - Finding the correct child view of ListView while clicking a button within it -

i conducting experiment seek , understand listview lifecycle.

i have listview, in each row have button , textview, experiment click button , have row painted green, here code:

edit:

i tried using array of booleans got same result

bundle com.ziv.test; import java.util.arraylist; import com.ziv.test.fr.madapter.viewholder; import android.content.context; import android.graphics.color; import android.os.bundle; import android.support.v4.app.fragment; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.button; import android.widget.linearlayout; import android.widget.listview; import android.widget.textview; public class fr extends fragment { arraylist<string> list; private listview lv; public fr() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_main, container, false); list = new arraylist<string>(); (int = 0; < 50; i++) { list.add("item number " + i); } lv = (listview) view.findviewbyid(r.id.listview1); madapter adapter = new madapter(list, lv); lv.setadapter(a dapter); homecoming view; } public class madapter extends baseadapter { arraylist<string> list; private layoutinflater inflater; private listview lv; // boolean[] button state: private boolean[] clickedbuttonpositions; public madapter(arraylist<string> list, listview lv) { this.list = list; this.lv = lv; inflater = (layoutinflater) getactivity().getsystemservice( context.layout_inflater_service); this.clickedbuttonpositions = new boolean[list.size()]; } @override public int getcount() { // todo auto-generated method stub homecoming list.size(); } @override public object getitem(int position) { // todo auto-generated method stub homecoming null; } @override public long getitemid(int position) { // todo auto-generated method stub homecoming 0; } @override public view getview(int position, view convertview, viewgroup parent) { // here write related views... // not forget reuse... viewholder vh; if (convertview == null) { vh = new viewholder(); convertview = inflater.inflate(r.layout.list_cell, null); vh.text = (textview) convertview.findviewbyid(r.id.list_test); vh.btn = (button) convertview.findviewbyid(r.id.list_button); convertview.settag(vh); } else { vh = (viewholder) convertview.gettag(); } vh.text.settext(list.get(position)); vh.btn.settag(r.id.list_button, position); vh.btn.setonclicklistener(onclickbutton); homecoming convertview; } public class viewholder { textview text; button btn; } private view.onclicklistener onclickbutton = new view.onclicklistener() { @override public void onclick(view v) { // v view received click event, no // need check. int pos = (int) v.gettag(r.id.list_button); linearlayout view = (linearlayout) lv.getchildat(pos); // switching 'clicked' value button - if want // button "turn off" view clickedbuttonpositions[pos] = !clickedbuttonpositions[pos]; // if don't want button "turn off" view utilize // line instead: // clickedbuttonpositions[pos] = true; // save button state in local variable, optional boolean clicked = clickedbuttonpositions[pos]; // if want "turn off" behavior utilize line '?:' // operator view.setbackgroundcolor(clicked ? color.green : color.ltgray); // if don't want "turn off" behavior do: } }; }

}

however, much disappointment, row click not 1 painted green, recycled row painted well, 1 outside screen. how can isolate desired row in click event?

i faced similar problem in project , extending base of operations adapter did. solution simple. goes this:

if(buttonclick)//any boolean variable indicating clicked { view.setbackgroundcolor(color.green); }else{ view.setbackgroundcolor(color.white); }

android listview onclicklistener baseadapter

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -