android - Can't get my ListView items to be highlighted when they are selected -
i have app hits rest service, gets json , places listview. main activity extends activity , not listactivity. can scroll through list items, cannot select (at least touch). listview through findviewbyid , have adapter extends simpleadapter. have set convertview inside adapter's getview() clickable. when this, can no longer click on list items in listview? did wrong?
public class app extends activity { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); jobslistview = (listview) findviewbyid(r.id.jobs_list); jobslistview.setadapter(new jobitemadapter(jobslistview.getcontext(), jobslist, r.layout.list_item, new string[] {"title","location"}, new int[] {r.id.job_title, r.id.job_location})); jobslistview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view arg1, int arg2, long arg3) { // todo auto-generated method stub log.i("jobslistview.setonitemclicklistener()"," item clicked: " + arg0.getselecteditemposition()); } }); } public class jobitemadapter extends simpleadapter { private list<hashmap<string,string>> jobslist; public jobitemadapter(context context, list<hashmap<string,string>> jobs, int resource, string[] from, int[] to) { super(context, jobs, resource, from, to); jobslist = jobs; } public view getview(int position, view convertview, viewgroup parent){ log.i(tag," in jobitemadapter getview() "); if (convertview == null) { //no previous views, inflate views convertview = getlayoutinflater().inflate(r.layout.list_item, parent, false); } //update textviews in layout textview title = (textview)convertview.findviewbyid(r.id.job_title); title.settext(jobslist.get(position).get("title")); textview location = (textview)convertview.findviewbyid(r.id.job_location); location.settext(jobslist.get(position).get("location")); //make view clickable convertview.setclickable(true); return convertview; } } }
probably can happening defining background color row, defined in r.layout.list_item, , background color not color defined in selector, example:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" > <shape android:shape="rectangle"> <solid android:color="@color/blue_button_press"/> </shape> </item> <item android:state_selected="true" > <shape android:shape="rectangle"> <solid android:color="@color/blue_button_focus" /> </shape> </item> <item android:state_focused="true" > <shape android:shape="rectangle"> <solid android:color="@color/blue_button_focus" /> </shape> </item> <item> <shape android:shape="rectangle" > <solid android:color="@color/white" /> </shape> </item> </selector>
in it, can define color each state of view, in case: focused , pressed. in order use it, in r.layout.list_item, set background normaly, example
android:background="@drawable/list_row_background"
considering xml file file described aboved named list_row_background , in drawable folder
and shouldn't set clickable, since clickable. hope solves problem :d cheers
Comments
Post a Comment