xml - How to get all selected checkbox from a ExpandableListView in Android? -
i have expandablelistview
has groups , itens, each iten has checkbox can selected , want know how can iterate on itens , values when click button.
so far have created list, can select checkboxes , going well, cant find solution values checkboxes when click button. questionary, when user finishes picking choices, submit values.
i have mainactivity
, expandablelistadapter
list adapter implementation , have 3 xml, activity_main.xml, list_group.xml , list_itens.xml.
here code far:
public class expandablelistadapter extends baseexpandablelistadapter { private context context; private list<string> listadeperguntas; private hashmap<string, list<string>> listaderespostas; private checkbox check; public expandablelistadapter(context context, list<string> listadeperguntas, hashmap<string, list<string>> listaderespostas) { this.context = context; this.listadeperguntas = listadeperguntas; this.listaderespostas = listaderespostas; } @override public int getgroupcount() { return listadeperguntas.size(); } @override public int getchildrencount(int groupposition) { return listaderespostas.get(listadeperguntas.get(groupposition)).size(); } @override public object getgroup(int groupposition) { return listadeperguntas.get(groupposition); } @override public object getchild(int groupposition, int childposition) { return listaderespostas.get(listadeperguntas.get(groupposition)).get(childposition); } @override public long getgroupid(int groupposition) { return groupposition; } @override public long getchildid(int groupposition, int childposition) { return childposition; } @override public boolean hasstableids() { return false; } @override public view getchildview(int groupposition, int childposition, boolean islastchild, view convertview, viewgroup parent) { string resposta = (string) getchild(groupposition, childposition); if(convertview == null) { layoutinflater inflater = (layoutinflater) this.context.getsystemservice(context.layout_inflater_service); convertview = inflater.inflate(r.layout.list_itens, null); } textview text = (textview) convertview.findviewbyid(r.id.listitem); text.settext(resposta); return convertview; } @override public view getchildview(final int groupposition, final int childposition, boolean islastchild, view convertview, viewgroup parent) { string resposta = (string) getchild(groupposition, childposition); if(convertview == null) { layoutinflater inflater = (layoutinflater) this.context.getsystemservice(context.layout_inflater_service); convertview = inflater.inflate(r.layout.list_itens, null); } textview text = (textview) convertview.findviewbyid(r.id.listitem); text.settext(resposta); checkbox check = (checkbox) convertview.findviewbyid(r.id.listcheckitem); //check.setfocusable(false); check.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { toast.maketext(buttonview.getcontext(), "selected! - question number: " + groupposition + "answer: " + childposition + "ischecked: " + ischecked, toast.length_short).show(); } }); return convertview; } @override public boolean ischildselectable(int groupposition, int childposition) { return true; }
}
my mainactivity
class:
public class mainactivity extends appcompatactivity implements view.onclicklistener{ private expandablelistview listview; private expandablelistadapter listadapter; private list<string> perguntas; private hashmap<string, list<string>> respostas; private button enviarrespostas; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); listview = (expandablelistview) findviewbyid(r.id.expandablelistview); carregardados(); listadapter = new expandablelistadapter(this, perguntas, respostas); listview.setadapter(listadapter); listview.setonchildclicklistener(new expandablelistview.onchildclicklistener() { @override public boolean onchildclick(expandablelistview parent, view v, int groupposition, int childposition, long id) { toast.maketext(mainactivity.this, "clicou no:" + groupposition + " item: " + childposition, toast.length_short).show(); return false; } }); listview.setongroupclicklistener(new expandablelistview.ongroupclicklistener() { @override public boolean ongroupclick(expandablelistview parent, view v, int groupposition, long id) { toast.maketext(mainactivity.this, "clicou no:" + groupposition, toast.length_short).show(); return false; } }); enviarrespostas = (button) findviewbyid(r.id.buttonenviarrespostas); enviarrespostas.setonclicklistener(this); } private void carregardados() { perguntas = new arraylist<>(); respostas = new hashmap<>(); perguntas.add("pertunta 1"); perguntas.add("pertunta 2"); perguntas.add("pertunta 3"); perguntas.add("pertunta 4"); list<string> respostaspergunta1 = new arraylist<>(); respostaspergunta1.add("sim"); respostaspergunta1.add("não"); respostaspergunta1.add("talvez"); list<string> respostaspergunta2 = new arraylist<>(); respostaspergunta2.add("10"); respostaspergunta2.add("20"); respostaspergunta2.add("30"); respostaspergunta2.add("40"); list<string> respostaspergunta3 = new arraylist<>(); respostaspergunta3.add("sim"); respostaspergunta3.add("não"); list<string> respostaspergunta4 = new arraylist<>(); respostaspergunta4.add("10"); respostaspergunta4.add("20"); respostaspergunta4.add("30"); respostaspergunta4.add("40"); respostaspergunta4.add("50"); respostaspergunta4.add("60"); respostas.put(perguntas.get(0), respostaspergunta1); respostas.put(perguntas.get(1), respostaspergunta2); respostas.put(perguntas.get(2), respostaspergunta3); respostas.put(perguntas.get(3), respostaspergunta4); } @override public void onclick(view v) { //todo }
}
and here 3 .xml files:
1) activity_main.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="match_parent" android:padding="10dp" android:weightsum="1"> <expandablelistview android:id="@+id/expandablelistview" android:layout_width="match_parent" android:layout_height="437dp" /> <button android:id="@+id/buttonenviarrespostas" android:layout_width="match_parent" android:layout_height="35dp" android:text="button" /> </linearlayout>
2) list_group.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="8dp" android:background="@android:color/white"> <textview android:id="@+id/header" android:paddingleft="? android:attr/expandablelistpreferreditempaddingleft" android:textsize="16dp" android:textcolor="@android:color/black" android:layout_width="match_parent" android:layout_height="/> </linearlayout>
3) list_itens.xml
<tablelayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:stretchcolumns="0"> <tablerow> <textview android:id="@+id/listitem" android:textsize="16dp" android:paddingleft="? android:attr/expandablelistpreferredchildpaddingleft" android:paddingtop="5dp" android:paddingbottom="5dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <checkbox android:id="@+id/listcheckitem" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginright="50dp" /> </tablerow> </tablelayout>
Comments
Post a Comment