android - Programmatically change value of disabled radio button in each row of a ListView -
i have listview 3 textviews , radio button in each row. retrieve state firebase database , show in radio button. don't know how access radio button change state. know should use custom adapter don't how to. can ?
this code time being :
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_grille); listview = (listview) findviewbyid(r.id._afficher); bus = new bus(threadenforcer.main); bus.register(this); listview.addheaderview(getlayoutinflater().inflate(r.layout.header, null, false)); loaddata(); } private void loaddata(){ databasereference mdb = firebasedatabase.getinstance().getreference("vfl"); mdb.addlistenerforsinglevalueevent(new valueeventlistener() { @override public void ondatachange(datasnapshot datasnapshot) { matrixcursor matrixcursor= new matrixcursor(columns); startmanagingcursor(matrixcursor); (datasnapshot snapshot : datasnapshot.getchildren()) { (datasnapshot snapsht : snapshot.getchildren()){ string s = snapsht.child("visiteur").getvalue().tostring(); if(s.equals(visiteur) || s.equals(visiteur1)){ matrixcursor.addrow(new object[] {count.incrementandget(), snapsht.child("date").getvalue().tostring(), snapsht.child("zone").getvalue().tostring(),snapsht.child("visité").getvalue().tostring() }); } } } bus.post(matrixcursor); } @override public void oncancelled(databaseerror databaseerror) { } }); } @subscribe public void answeravailable( matrixcursor matrixcursor) { // todo: react event somehow! simplecursoradapter adapter = new simplecursoradapter(getapplicationcontext(), r.layout.ligne_afficher, matrixcursor, from, to, 0); listview.setadapter(adapter); }
see example using customadapter extending baseadapter class:
1. listview activity layout
<?xml version="1.0" encoding="utf-8"?> <listview xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="in.oxyzen.loc.youractivity" android:id="@+id/listview"> </listview>
2. listview's single_item_layout.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:text="tv1" android:id="@+id/tv1"/> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:text="tv2" android:id="@+id/tv2"/> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:text="tv3" android:id="@+id/tv3"/> <radiobutton android:layout_width="match_parent" android:layout_height="wrap_content" android:text="button" android:id="@+id/radiobutton"/> </linearlayout>
3. (listview's activity) youractivity.java
public class youractivity extends appcompatactivity implements childeventlistener { listview listview; customadapter adapter; databasereference yourreference; progressdialog dialog; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); yourreference =firebasedatabase.getinstance().getreference().child("your_reference"); dialog = new progressdialog(this); dialog.setmessage("loading..."); dialog.setcancelable(false); setcontentview(r.layout.activity_your); listview = (listview)findviewbyid(r.id.listview); adapter = new customadapter(); listview.setadapter(adapter); dialog.show(); yourreference.addchildeventlistener(this); } @override public void onchildadded(datasnapshot datasnapshot, string s) { dialog.dismiss(); adapter.additem(datasnapshot); } @override public void onchildchanged(datasnapshot datasnapshot, string s) { } @override public void onchildremoved(datasnapshot datasnapshot) { } @override public void onchildmoved(datasnapshot datasnapshot, string s) { } @override public void oncancelled(databaseerror databaseerror) { } }
4. customadapter.java class listview
public class customadapter extends baseadapter { arraylist<datasnapshot> values; customadapter(){ values = new arraylist<>(); } @override public int getcount() { return values.size(); } @override public object getitem(int position) { return values.get(position); //you can datasnapshot interacting listview items per position use in listview's activity. //you can return null here. } @override public long getitemid(int position) { return 0; } @override public view getview(int position, view convertview, viewgroup parent) { datasnapshot snapshot = values.get(position);//use snapshot retrieve values textviews , radiobutton. ourviewholder holder =new ourviewholder(parent.getcontext(),parent); holder.tv1.settext(snapshot.getkey()); holder.tv2.settext("text textview 2"); holder.tv3.settext("text textview 3"); holder.button.setchecked(true);// manipulate radio button per wish here. checked example. return holder.itemview; } //custom method: notify , update adapter automatically if new child adds in firebase database. void additem(datasnapshot snapshot){ values.add(snapshot); notifydatasetchanged(); } //custom class: using ourviewholder pattern keeps alive java's oop concept i.e. 1 object per listview item. //inflate single item's layout in here , find components of item. private class ourviewholder { view itemview; textview tv1,tv2,tv3; radiobutton button; ourviewholder(context context, viewgroup parent){ itemview = layoutinflater.from(context).inflate(r.layout.single_item_layout,parent,false); tv1=(textview)itemview.findviewbyid(r.id.tv1); tv2=(textview)itemview.findviewbyid(r.id.tv2); tv3=(textview)itemview.findviewbyid(r.id.tv3); button=(radiobutton)itemview.findviewbyid(r.id.radiobutton); } } }
Comments
Post a Comment