android - Set background row when click row in listview error -



android - Set background row when click row in listview error -

at moment,this code. want create listview ,when click row in listview,background of row alter red.when row's color red,click row's color alter white. run app,click row,then scroll listview,don't color of row clicked alter red,some below row alter red.

adapter

public class itembaseadapter extends baseadapter { private string[] marrayitems; private context context; public itembaseadapter(context context, string[] arrays) { this.context = context; this.marrayitems = arrays; } @override public int getcount() { // todo auto-generated method stub homecoming marrayitems.length; } @override public object getitem(int arg0) { // todo auto-generated method stub homecoming null; } @override public long getitemid(int arg0) { // todo auto-generated method stub homecoming 0; } @override public view getview(int position, view convertview, viewgroup arg2) { // todo auto-generated method stub final viewholder holder; if (convertview == null) { convertview = layoutinflater.from(context).inflate( r.layout.item_layout, null); holder = new viewholder(); holder.textviewitem = (textview) convertview .findviewbyid(r.id.textviewitemlist); holder.layoutitem = (linearlayout) convertview .findviewbyid(r.id.layoutitemlist); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } holder.textviewitem.settext(marrayitems[position]); holder.layoutitem.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub if (holder.ischecklayout == false) { holder.ischecklayout = true; holder.layoutitem.setbackgroundcolor(color.red); } else { holder.ischecklayout = false; holder.layoutitem.setbackgroundcolor(color.white); } } }); homecoming convertview; } static class viewholder { textview textviewitem; linearlayout layoutitem; boolean ischecklayout = false; }

}

and activity

public class mainactivity extends activity {

private listview mlistview; string[] arraylist = { "a", "b", "c", "d", "e", "a", "b", "c", "d", "e", "a", "b", "c", "d", "e", "a", "b", "c", "d", "e", "a", "b", "c", "d", "e", "a", "b", "c", "d", "a", "b", "c", "d", "e", "a", "b", "c", "d", "e", "a", "b", "c", "d", "e", "d", "e", "a", "b", "c", "d", "a", "b", "c", "d", "e", "a", "b", "c", "d", "e", "a", "b", "c", "d", "e" }; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mlistview = (listview) findviewbyid(r.id.listviewitem); mlistview.setadapter(new itembaseadapter(this, arraylist)); }

}

and xml of item layout

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <linearlayout android:id="@+id/layoutitemlist" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal" > <textview android:id="@+id/textviewitemlist" android:layout_width="wrap_content" android:text="@string/action_settings" android:layout_height="wrap_content" android:layout_margin="10dp" /> </linearlayout>

i don't know ,what cause it? how must do?

getview called multiple times (example : when listview beingness scrolled), must if-else operation not when button clicked. seek alter code :

holder.textviewitem.settext(marrayitems[position]); // add together if-else outside onclick if (ischecked[position] == false) { holder.layoutitem.setbackgroundcolor(color.white); } else { holder.layoutitem.setbackgroundcolor(color.red); } holder.layoutitem.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub if (ischecked[position] == false) { ischecked[position] = true; } else { ischecked[position] = false; } notifydatasetchanged(); // phone call getview 1 time again } }); homecoming convertview; }

android listview background android-adapter

Comments

Popular posts from this blog

php - Edges appear in image after resizing -

ios8 - iOS custom keyboard - preserve state between appearances -

Delphi change the assembly code of a running process -