Hello I am trying to create a custom expandable listview, but the documentation on the adapter is very confusing and diverse. As usual, I have a hashmap to read the json array and then I put it on the list. The group of each child is sent within the same array in the key “GRUPO”, on the last position. Can someone please give me the adapter implementation to this? The code from the json follows below:
if (tipopergunta.contentEquals("BOOLEANO") || tipopergunta.contentEquals("ESCMULTIPLA") || tipopergunta.contentEquals("ESCUNICA")){
HashMap<String,String> childdados = new HashMap<String,String>();
childdados.put("GRUPO", "Dados");
childdados.put("TIPOPERGUNTA", tipopergunta);
childdados.put("PERGUNTA", pergunta);
childdados.put("BREVEDESIGNACAO", brevedesc);
childdados.put("ESTADO",estado);
childdados.put("INSTRUCOES",instrucoes);
childdados.put("IDPERGUNTA",idpergunta);
Log.d("childdados",""+childdados.toString());
list.add(childdados);
}
if (tipopergunta.contentEquals("OPINIAO")){
HashMap<String,String> childdados = new HashMap<String,String>();
childdados.put("GRUPO", "Opinião");
childdados.put("TIPOPERGUNTA", tipopergunta);
childdados.put("PERGUNTA", pergunta);
childdados.put("BREVEDESIGNACAO", brevedesc);
childdados.put("ESTADO",estado);
childdados.put("INSTRUCOES",instrucoes);
childdados.put("IDPERGUNTA",idpergunta);
Log.d("opiniaolist",childdados.toString());
list.add(childdados);
}
if (tipopergunta.contentEquals("FOTOGRAFIA")){
HashMap<String,String> childdados = new HashMap<String,String>();
childdados.put("GRUPO", "Fotografia");
childdados.put("TIPOPERGUNTA", tipopergunta);
childdados.put("PERGUNTA", pergunta);
childdados.put("BREVEDESIGNACAO", brevedesc);
childdados.put("ESTADO",estado);
childdados.put("INSTRUCOES",instrucoes);
childdados.put("IDPERGUNTA",idpergunta);
Log.d("fotolist",childdados.toString());
list.add(childdados);
}
This is what helped me understand the expandable list view adapter.
On a extendable list view you have always parents and children. When you click on a parent you see its children.
So the adapter needs 3 things, mainly.
A list of parents.
A list of children (which can be for example an array of arrays)
how to draw the children and how to draw the parents.
you should have an array of parents:
String[] parents = {"parent1, "parent2");and an array of arrays of childern, which, each array is children of one parent.
on the method getGroup you should return the parent you want for a given row:
on the method getChild you should return the child you want:
on the methods getChildView() you should inflate the layout you want (for example a xml with a textview) and set the text with the corresponding child. Note that here you don’t need to worry about which child it is already passed on the arguments:
the same can be said about the parent method:
you can set all kinds of layouts for the parent and the children. Just use an inflater to inflate the xml from the layout folders. and then use the resource id’s to fill the views with your widgets.
hope this helps you adaptiing your data to this snippet.