Reputation: 470
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);
}
Upvotes: 1
Views: 4078
Reputation: 6795
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.
String [][] children = {{child_A_Parent1, child_B_Parent1}, {child_A_Parent2, child_B_parent2}};
on the method getGroup you should return the parent you want for a given row:
public Object getGroup(int groupPosition) {
return parents[groupPosition];
}
on the method getChild you should return the child you want:
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
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:
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
//inflate a the layout and get the text view
tv.setText(children[grouposition][childPosition];
..}
the same can be said about the parent method:
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
//inflate a the layout and get the text view
tvParent.setText(parents[groupPosition];
}
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.
Upvotes: 1