listview - Android Custom SimpleAdapter Background Color -
i have been trying implement getting listview item background color change based on state using custom simpleadapter. following code references found here , on other sites. have partially working, not completely.
first, code:
the xml over-all gui containing listview:
<?xml version="1.0" encoding="utf-8"?> <relativelayout android:id="@+id/widget37" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffffff" xmlns:android="http://schemas.android.com/apk/res/android" > <progressbar android:id="@+id/progressbar" style="?android:attr/progressbarstylelarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_centervertical="true" /> <textview android:id="@+id/textviewmessage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_marginleft="10dp" android:layout_margintop="10dp" android:text="building list..." android:textappearance="?android:attr/textappearancemedium" android:textcolor="#ff000000" android:textsize="22sp" /> <relativelayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/textviewmessage" android:layout_alignparentleft="true" android:layout_margintop="20dp"> <listview android:id="@id/listlocations" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="20dp" android:layout_marginleft="10dp" android:cachecolorhint="#00000000" android:choicemode="singlechoice" android:clickable="true" android:scrollingcache="false" android:background="@color/transparent" android:listselector="@drawable/listitem_selector"> </listview> </relativelayout> <button android:id="@+id/buttontryagain" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_alignparenttop="true" android:text="try again" /> based on other postings, notice using listitem_selector background listview individual items can reflect state in different colors.
next xml listitem_selector:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:drawable="@color/itemselected" /> <item android:state_pressed="true" android:drawable="@color/itempressed" /> <item android:drawable="@color/text_white" />
here 'wrinkle' due fact each item comprised of 4 textboxes - adapter built utilize layout list_parkers_tmp
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="60dp" android:background="@drawable/listitem_selector"> <imageview android:id="@+id/logo" android:layout_width="40dp" android:layout_height="50dp" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:src="@drawable/hangtag" /> <textview android:id="@+id/textviewpermit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_torightof="@+id/logo" android:text="permit number" android:textstyle="bold" android:textcolor="#ff00aa00" android:textsize="22dp" /> <textview android:id="@+id/textviewcategory" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_torightof="@+id/textviewpermit" android:paddingleft="10dp" android:text="category" android:textcolor="#ff555555" android:textsize="16dp" /> <textview android:id="@+id/textviewcars" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/logo" android:layout_alignparentright="true" android:layout_torightof="@+id/logo" android:text="textview" android:textcolor="#ff000000" android:textsize="18dp" /> <textview android:id="@+id/textstatus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_alignparenttop="true" android:paddingleft="10dp" android:text="status" android:textstyle="bold" android:textcolor="#ff0000" android:textsize="16dp" /> code within activity:
// create our selectedadapter selectedadapter = new selectedadapter(getbasecontext(), itemlist, r.layout.list_parkers_tmp, new string[]{"permit", "status", "category", "cars"}, new int[]{r.id.textviewpermit, r.id.textstatus, r.id.textviewcategory, r.id.textviewcars}); selectedadapter.notifydatasetchanged(); listlocations.setadapter(selectedadapter); and custom simpleadapter:
public class selectedadapter extends simpleadapter { private int[] mto; private string[] mfrom; private viewbinder mviewbinder; private list<? extends map<string, ?>> mdata; private int mresource; private int mdropdownresource; private layoutinflater minflater; public selectedadapter(context context, list<? extends map<string, string>> data, int resource, string[] from, int[] to) { super(context, data, resource, from, to); mdata = data; mresource = mdropdownresource = resource; mfrom = from; mto = to; minflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); } /** * @see android.widget.adapter#getview(int, view, viewgroup) */ // used keep selected position in listview private int selectedpos = -1; // init value not-selected public void setselectedposition(int pos) { selectedpos = pos; // inform view of change notifydatasetchanged(); } public int getselectedposition() { return selectedpos; } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { //view v = convertview; view v = super.getview(position, convertview, parent); // inflate view if it's null if (v == null) { layoutinflater vi = (layoutinflater) config.context.getsystemservice(context.layout_inflater_service); //layoutinflater vi // = (layoutinflater) this.getcontext().getsystemservice(context.layout_inflater_service); v = vi.inflate(r.layout.list_parkers_tmp, null); } // text view string textviewstr = ""; string categorytext = ""; textviewstr = this.getitem(position).tostring(); // of listview item's text values textview viewpermit = (textview) v.findviewbyid(r.id.textviewpermit); categorytext = parseitemstr(textviewstr,"permit=",""); viewpermit.settext(categorytext); textview status = (textview) v.findviewbyid(r.id.textstatus); categorytext = parseitemstr(textviewstr,"status=","cars="); status.settext(categorytext); textview viewcategory = (textview) v.findviewbyid(r.id.textviewcategory); categorytext = parseitemstr(textviewstr,"category=","permit="); viewcategory.settext(categorytext); textview viewcars = (textview) v.findviewbyid(r.id.textviewcars); categorytext = parseitemstr(textviewstr,"cars=","category="); viewcars.settext(categorytext); // change row color based on selected state if (selectedpos == position) { v.setselected(true); v.setpressed(true); v.setbackgroundcolor(r.color.amber_800); } else { v.setselected(false); v.setpressed(false); v.setbackgroundcolor(r.color.text_white); } /* // use other .tostring() myclass myobj = (myclass)this.getitem(position); label.settext(myobj.myreturnsstring()); */ return (v); } public string parseitemstr (string itemstr, string category1, string category2) { itemstr = itemstr.replace("}",""); itemstr = itemstr.replace("{",""); int loc1 = itemstr.indexof((category1)); int loc2 = 0; if (!category2.equals("")) { loc2 = itemstr.indexof((category2)); } else { loc2 = itemstr.length(); } string shortstr = itemstr.substring(loc1,loc2); shortstr = shortstr.replace(category1,"").trim(); int len = shortstr.length(); if (len > 0){ if (shortstr.substring(len-1,len).equals(",")) { shortstr = shortstr.substring(0,math.max(0,len-1)); } } return shortstr; } }
i can watch code execute , getview() executes expected.
first, when listview created (no specific item selected), individual item's background color not transparent expected, gray. that's not right.
then, when item selected getview() code portion selectedpos == position executes intended.
no background color change results it.
have totally messed things or have missed few things?
try using setactivated.
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_activated="true" android:drawable="@color/amber_800" /> <item android:state_pressed="true" android:drawable="@color/itempressed" /> <item android:drawable="@color/transparent" /> </selector> then activate , deactivate
// change row color based on selected state if (selectedpos == position) { v.setactivated(true); } else { v.setactivated(false); }
Comments
Post a Comment