javascript - How to replace the selected row icon with another icon on list item on click in android -
i have tried change icon of row on list item click no success.i followed suggestions other s/o questions got ended no success.i have tried methods like
@override public void onitemclick(adapterview<?> parent, view view, int position, long id) { imageview imageview = ((imageview) view); imageview.setimageresource(r.drawable.open); imageview.invalidate();
but nothing has worked far.so had post here after many days of research.so help,suggestions or referrals appreciated.
my adapter:
public class contactlistadapter extends baseadapter { context context; arraylist<contactlistitems> contactlist; public contactlistadapter(context context, arraylist<contactlistitems> list) { this.context = context; contactlist = list; } @override public int getcount() { return contactlist.size(); } @override public object getitem(int position) { return contactlist.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup arg2) { contactlistitems contactlistitems = contactlist.get(position); if (convertview == null) { layoutinflater inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); convertview = inflater.inflate(r.layout.contact_list_row, null); } textview tvslno = (textview) convertview.findviewbyid(r.id.tv_slno); tvslno.settext(contactlistitems.getid()); textview tvname = (textview) convertview.findviewbyid(r.id.tv_name); tvname.settext(contactlistitems.getname()); textview tvphone = (textview) convertview.findviewbyid(r.id.tv_phone); tvphone.settext(contactlistitems.getsalary()); return convertview; }}
where populate listview:
public class inbox extends fragment { string parsingdta= "{\"employee\":[{\"id\":\"101\",\"name\":\"class:\",\"salary\":\"spc112 has been postponed wed 2.30pm @sr6 \"},{\"id\":\"102\",\"name\":\"exams:\",\"salary\":\"there change of venue of spy312 exam nct7 nct5\"},{\"id\":\"103\",\"name\":\"exams:\",\"salary\":\"there slight change of venue of spy312 exam nct7 nct5\"},{\"id\":\"104\",\"name\":\"class:\",\"salary\":\"there slight change of venue of spy312 exam nct5 nct6\"}]}"; string str=""; listview listview; database database; @override public void onattach(activity activity){ super.onattach(activity); database=new database(activity); } @override public view oncreateview(layoutinflater inflater,viewgroup container, bundle savedinstancestate) { view rootview=inflater.inflate(r.layout.inbox,container,false); database.getwritabledatabase(); listview=(listview)rootview.findviewbyid(r.id.listview); try { jsonobject jsonobject=new jsonobject(parsingdta); jsonarray jsonarray= jsonobject.getjsonarray("employee"); for(int i=0;i<jsonarray.length();i++) { jsonobject jsonobject1=jsonarray.getjsonobject(i); string id=jsonobject1.getstring("id").tostring(); string name=jsonobject1.getstring("name").tostring(); string salary=jsonobject1.getstring("salary").tostring(); database.insertdata(id,name,salary); str+="\n employee"+i+ "\n name:"+name+"\n id:"+id+"\n salary:" +salary+"\n"; //textview1.settext(str); } } catch(jsonexception e) { e.printstacktrace(); } arraylist<contactlistitems> contactlist =database.fetchdata(); contactlistadapter contactlistadapter = new contactlistadapter( getactivity().getapplicationcontext(), contactlist); listview.setadapter(contactlistadapter); listview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { imageview imageview = ((imageview) view); imageview.setimageresource(r.drawable.open); imageview.invalidate(); intent intent=new intent(getactivity().getapplicationcontext(),inboxdetails.class); startactivity(intent); } }); setlistviewheightbasedonchildren(listview); return rootview; } private void setlistviewheightbasedonchildren(listview listview) { contactlistadapter contactlistadapter = (contactlistadapter) listview.getadapter(); if (contactlistadapter == null) return; int desiredwidth = view.measurespec.makemeasurespec(listview.getwidth(), view.measurespec.unspecified); int totalheight = 0; view view = null; (int = 0; < contactlistadapter.getcount(); i++) { view = contactlistadapter.getview(i, view,listview); if (i == 0) view.setlayoutparams(new viewgroup.layoutparams(desiredwidth, viewgroup.layoutparams.wrap_content)); view.measure(desiredwidth, view.measurespec.unspecified); totalheight += view.getmeasuredheight(); } viewgroup.layoutparams params = listview.getlayoutparams(); params.height = totalheight + (listview.getdividerheight() * (contactlistadapter.getcount() - 1)); listview.setlayoutparams(params); listview.requestlayout(); } }
contactlistitems:
public class contactlistitems { string id; string name; string salary; public imageview img; public string getid() { return id; } public void setid(string id) { this.id = id; } public string getname() { return name; } public void setname( string name) { this.name = name; } public string getsalary() { return salary; } public void setsalary(string salary) { this.salary = salary; } }
contact_list_row.xml:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:paddingbottom="5dp" android:paddingtop="5dp" android:background="@drawable/list_background_selector" android:layout_height="match_parent"> <textview android:id="@+id/tv_slno" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="id" android:textcolor="#000000" android:textsize="10dp" android:typeface="monospace" android:layout_alignparenttop="true" android:layout_alignparentright="true" android:layout_alignparentend="true" android:layout_marginright="42dp" android:layout_marginend="42dp" /> <textview android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="title" android:textcolor="#000000" android:textappearance="?android:attr/textappearancemedium" android:layout_marginleft="14dp" android:layout_marginstart="14dp" android:layout_alignparenttop="true" android:layout_torightof="@+id/imageview" android:layout_toendof="@+id/imageview" /> <imageview android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/list" android:id="@+id/imageview" android:paddingtop="6dp" android:paddingbottom="6dp" android:layout_gravity="center_horizontal" android:layout_alignparenttop="true" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_marginleft="16dp" android:layout_marginstart="16dp" /> <textview android:id="@+id/tv_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="description" android:singleline="true" android:textcolor="#000000" android:textappearance="?android:attr/textappearancemedium" android:layout_alignbottom="@+id/imageview" android:layout_alignleft="@+id/tv_name" android:layout_alignstart="@+id/tv_name" /> </relativelayout>
try
@override public void onitemclick(adapterview<?> parent, view view, int position, long id) { /*******************************************************************/ imageview imageview = (imageview)view.findviewbyid(r.id.imageview); /*******************************************************************/ imageview.setimageresource(r.drawable.open); imageview.invalidate();
Comments
Post a Comment