android - Earlier I implemented checkbox in listview, how to replace it with RadioButtons with same implementation -
i have listview earlier implemented checkbox, want replace radiobuttons, seems earlier implementation won't work. changes need make replace checkbox implementation radiobuttons? below working code :
elistcbox.java
package aexp.elistcbox; import java.util.arraylist; import android.app.expandablelistactivity; import android.os.bundle; import android.util.log; import android.view.view; import android.widget.button; import android.widget.checkbox; import android.widget.expandablelistview; import android.widget.radiobutton; public class elistcbox extends expandablelistactivity { private static final string log_tag = "elistcbox2"; private coloradapter explistadapter; /** called when activity first created. */ @override public void oncreate(bundle icicle) { super.oncreate(icicle); setcontentview(r.layout.main); arraylist<string> groupnames = new arraylist<string>(); groupnames.add("abc"); groupnames.add("def"); groupnames.add("ghi"); arraylist<arraylist<item>> colors = new arraylist<arraylist<item>>(); arraylist<item> color = new arraylist<item>(); color = new arraylist<item>(); color.add(new sectionitem("please select one")); color.add( new entryitem( "abc", false ) ); color.add( new entryitem( "def", true ) ); color.add( new entryitem( "ghi", true )); color.add( new entryitem( "jkl", false )); color.add(new sectionitem("please select one")); color.add( new entryitem( "abc", false )); color.add( new entryitem( "def", false )); color.add( new entryitem( "ghi", false )); color.add( new entryitem( "jkl", false )); colors.add( color ); explistadapter = new coloradapter( this,groupnames, colors ); setlistadapter( explistadapter ); final button button = (button) findviewbyid(r.id.submitbutton); button.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { // data } }); } public void oncontentchanged () { super.oncontentchanged(); log.d( log_tag, "oncontentchanged" ); } public boolean onchildclick( expandablelistview parent, view v, int groupposition, int childposition, long id) { log.d( log_tag, "onchildclick: "+childposition ); radiobutton cb = (radiobutton)v.findviewbyid( r.id.radiobutton ); if( cb != null ) cb.toggle(); return false; } }
coloradapter.java
package aexp.elistcbox; import java.util.arraylist; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseexpandablelistadapter; import android.widget.checkbox; import android.widget.radiobutton; import android.widget.textview; public class coloradapter extends baseexpandablelistadapter { private context context; private arraylist<string> groups; private arraylist<arraylist<item>> colors; private layoutinflater inflater; public coloradapter(context context, arraylist<string> groups, arraylist<arraylist<item>> colors ) { this.context = context; this.groups = groups; this.colors = colors; inflater = layoutinflater.from( context ); } public object getchild(int groupposition, int childposition) { return colors.get( groupposition ).get(childposition ); } public long getchildid(int groupposition, int childposition) { return (long)( groupposition*1024+childposition ); // max 1024 children per group } public view getchildview(int groupposition, int childposition, boolean islastchild, view convertview, viewgroup parent) { view v = convertview; item c = (item)getchild( groupposition, childposition ); /*if( color != null ) color.settext( c.getcolor() ); checkbox cb = (checkbox)v.findviewbyid( r.id.check1 ); cb.setchecked( c.getstate() );*/ if (c != null) { if(c.issection()){ sectionitem si = (sectionitem)c; v = inflater.inflate(r.layout.list_item_section, parent,false); final textview sectionview = (textview) v.findviewbyid(r.id.list_item_section_text); sectionview.settext(si.gettitle()); } else { entryitem ei = (entryitem) c; v = inflater.inflate(r.layout.child_row, parent, false); textview color = (textview) v.findviewbyid(r.id.childname); if (color != null) color.settext(ei.getcolor()); radiobutton cb = (radiobutton) v.findviewbyid(r.id.radiobutton); cb.setchecked(ei.getstate()); } } return v; } public int getchildrencount(int groupposition) { return colors.get( groupposition ).size(); } public object getgroup(int groupposition) { return groups.get( groupposition ); } public int getgroupcount() { return groups.size(); } public long getgroupid(int groupposition) { return (long)( groupposition*1024 ); // consistent getchildid } public view getgroupview(int groupposition, boolean isexpanded, view convertview, viewgroup parent) { view v = null; if( convertview != null ) v = convertview; else v = inflater.inflate(r.layout.group_row, parent, false); string gt = (string)getgroup( groupposition ); textview colorgroup = (textview)v.findviewbyid( r.id.childname ); if( gt != null ) colorgroup.settext( gt ); return v; } public boolean hasstableids() { return true; } public boolean ischildselectable(int groupposition, int childposition) { return true; } public void ongroupcollapsed (int groupposition) {} public void ongroupexpanded(int groupposition) {} }
entryitem.java
package aexp.elistcbox; public class entryitem implements item { public string color = null; public boolean state = false; public entryitem( string color, boolean state ) { this.color = color; this.state = state; } public string getcolor() { return color; } public boolean getstate() { return state; } @override public boolean issection() { // todo auto-generated method stub return false; } @override public boolean isitem() { // todo auto-generated method stub return true; } }
item.java
package aexp.elistcbox; public interface item { public boolean issection(); public boolean isitem(); }
sectionitem.java
package aexp.elistcbox; public class sectionitem implements item{ private final string title; public sectionitem(string title) { this.title = title; } public string gettitle(){ return title; } @override public boolean issection() { return true; } @override public boolean isitem() { // todo auto-generated method stub return false; } }
child_row.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <textview android:id="@+id/childname" android:paddingleft="20px" android:focusable="false" android:textsize="19dp" android:textstyle="italic" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <linearlayout android:layout_height="match_parent" android:layout_width="fill_parent" android:gravity="right"> <radiobutton android:id="@+id/radiobutton" android:focusable="false" android:layout_marginright="20dip" android:layout_height="wrap_content" android:layout_width="wrap_content" /> </linearlayout> </linearlayout>
group_row.xml
<?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" android:padding="8dp" android:background="#000000"> <textview android:id="@+id/childname" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingleft="10dip" android:textsize="17dp" android:textcolor="#f9f93d"/> </linearlayout>
list_item_section.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#e7ebee" android:orientation="vertical"> <textview android:id="@+id/list_item_section_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.5" android:gravity="center_vertical" android:paddingtop="10dip" android:paddingbottom="7dip" android:paddingleft="15dip" android:textappearance="?android:attr/textappearancemedium" android:textcolor="#737577" android:textsize="18sp" /> </linearlayout>
main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f4f4f4" android:orientation="vertical" > <textview android:id="@+id/question" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margintop="20dip" android:paddingleft="10dp" android:paddingstart="10dp" android:text="please select" android:textcolor="#000" android:textsize="17dp" /> <expandablelistview android:id="@+id/android:list" android:layout_margintop="10dip" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:cachecolorhint="#00000000" /> <button android:id="@+id/submitbutton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="submit" /> </linearlayout>
Comments
Post a Comment