0
public class ExpandableListAdapters extends BaseExpandableListAdapter { 

private Context _context; 
List<String> group_data; 
List<String> child_data; 

public ExpandableListAdapters(Context context, List<String> listDataHeader, 
          HashMap<String, List<String>> listChildData) { 
    this._context = context; 
    Log.e("HEADER SIZE", "" + listDataHeader.size() + "..." + listChildData.size()); 

} 

public ExpandableListAdapters(ServicesFragment servicesFragment, List<String> service_names,List<String> service_desc) { 
    this._context = servicesFragment.getActivity(); 
    this.group_data = service_names; 
    this.child_data=service_desc; 
} 

@Override 
public Object getChild(int groupPosition, int childPosititon) { 
    return child_data.get(groupPosition); 
} 

@Override 
public long getChildId(int groupPosition, int childPosition) { 
    return childPosition; 
} 

@Override 
public View getChildView(int groupPosition, final int childPosition, 
         boolean isLastChild, View convertView, ViewGroup parent) { 
    Log.e("SERVICE LIST","...."+child_data.get(groupPosition)); 
    String description = child_data.get(groupPosition); 

    //final Double price = data.get(groupPosition).getProducts().get(childPosition).getPrice(); 
    Log.e("ADAPTER", "........" + description); 
    if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) this._context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.list_item, null); 
    } 

    TextView txtListChild = (TextView) convertView 
      .findViewById(R.id.lblListItem); 
    if(!description.equals(null)){ 
     txtListChild.setText(description);/*+ " : Rs." + price);*/ 
    } 

    return convertView; 
} 

@Override 
public int getChildrenCount(int groupPosition) { 
    return child_data.size(); 
} 

@Override 
public Object getGroup(int groupPosition) { 
    return group_data.get(groupPosition); 
} 

@Override 
public int getGroupCount() { 
    return group_data.size(); 
} 

@Override 
public long getGroupId(int groupPosition) { 
    return groupPosition; 
} 

@Override 
public View getGroupView(int groupPosition, boolean isExpanded, 
         View convertView, ViewGroup parent) { 
    String headerTitle = group_data.get(groupPosition); 
    if (convertView == null) { 
     LayoutInflater infalInflater = (LayoutInflater) this._context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = infalInflater.inflate(R.layout.list_group, null); 
    } 

    TextView lblListHeader = (TextView) convertView 
      .findViewById(R.id.lblListHeader); 
    lblListHeader.setTypeface(null, Typeface.BOLD); 
    lblListHeader.setText(headerTitle); 

    return convertView; 
} 

@Override 
public boolean hasStableIds() { 
    return false; 
} 

@Override 
public boolean isChildSelectable(int groupPosition, int childPosition) { 
    return true; 
} 
} 

Ce qui précède est mon code. L'élément enfant est créé deux fois sous chaque élément de groupe. Dans ma liste de groupes et d'enfants, il y a deux éléments. En fait, le résultat attendu pour mon code ci-dessus est:Article enfant créé deux fois dans ExpandableListView Android

Groupe1 child1 Group2 CHILD2

Je ne peux pas trouver l'erreur. S'il vous plaît aidez-moi quelqu'un.

Répondre

0

Remplacer cette méthode getChild à:

@Override 
public Object getChild(int groupPosition, int childPosititon) { 
    return child_data.get(groupPosition); 
} 

CORRECTE:

@Override 
public Object getChild(int groupPosition, int childPosititon) { 
     return child_data.get(group_data.get(groupPosition)) 
       .get(childPosititon); 
} 

De plus,

@Override 
    public int getChildrenCount(int groupPosition) { 
     return child_data.get(group_data.get(groupPosition)) 
       .size(); 
    } 
+0

indique l'erreur « get (int) dans la liste ne peut pas être appliquée à (java.lang.String) » –

+0

sur lequel vous en ligne obtenez cette erreur –

+0

se réfèrent cet exemple - http: //www.androidhive.info/2013/07/android-expandable-list-view-tutorial/ –

0
public ExpandableListAdapters(Context context, List<String> listDataHeader, 
          HashMap<String, List<String>> listChildData) { 
    this._context = context; 
    Log.e("HEADER SIZE", "" + listDataHeader.size() + "..." + listChildData.size()); 

} 

Les deux champs group_data et child_data sont de type List<String>, vous n'avez donc aucune information sur le nombre d'éléments enfants réellement disponibles pour chaque groupe. Vous devriez utiliser les types déclarés dans votre constructeur. List<String> group_data; et HashMap<String, List<String>> child_data, `

HashMap<String, List<String>>: Le premier paramètre String représente votre élément de groupe. Le second paramètre List<String> contient vos éléments enfants pour ce groupe.

@Override 
public int getChildrenCount(int groupPosition) { 
    return child_data.size(); 
} 

Dans cette méthode, vous devez dire la ExandableListView, combien d'éléments enfants sont disponibles pour le groupe à groupPosition. Donc, vous devez spécifier le nombre d'éléments enfants sont disponibles pour le groupe à groupPosition:

@Override 
public int getChildrenCount(int groupPosition) { 
    return child_data.get(group_data.get(groupPosition)).size(); 
} 
+0

montre l'erreur "get (int) dans la liste ne peut pas être appliqué à (java.lang.String)" –

+0

a modifié ma réponse – aschattney

+0

résolu mon problème en faisant référence http://www.androidhive.info/2013/07/android-expandable -list-view-tutorial/ merci beaucoup pour l'aide :) –

0

Votre code est ci-dessous. En cela, vous prenez des données de child_data avec l'index de la position du groupe. Ainsi, pour un groupe, tous les enfants ont la même position de groupe,

@Override 
    public Object getChild(int groupPosition, int childPosititon) { 
    return child_data.get(groupPosition); 
    } 

Le code a donc été remplacé par la position enfant.

@Override 
public Object getChild(int groupPosition, int childPosititon) { 
return child_data.get(childPosititon); 
} 

Cela vous aidera.

+0

J'ai changé le get (groupPostition) pour obtenir (childPosition). mais pas de changement –

+0

Salut que signifie ce constructeur? ExpandableListAdapters publics (contexte Contexte, Liste listDataHeader, HashMap > listChildData) { this._context = contexte; Log.e ("HEADER SIZE", "" + listDataHeader.size() + "..." + listChildData.size()); } – Godwin

+1

résolu mon problème en faisant référence http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/ merci beaucoup pour l'aide :) –

0

Si cela ne fonctionne pas pour vous

@Override 
public Object getChild(int groupPosition, int childPosititon) { 
return child_data.get(childPosititon); 
} 

prendre le premier constructeur

public ExpandableListAdapters(Context context, List<String> listDataHeader, 
         HashMap<String, List<String>> listChildData) { 
    this._context = context; 
    Log.e("HEADER SIZE", "" + listDataHeader.size() + "..." +   listChildData.size()); 

}

et remplacer le premier code

@Override 
public Object getChild(int groupPosition, int childPosititon) { 
String groupKey=getGroup(groupPosition); 
return listChildData.get(groupKey).get(childPosition); 
} 

Et aussi remplacer la fonction

@Override 
public int getChildrenCount(int groupPosition) { 
String groupKey=getGroup(groupPosition); 
return listChildData.get(groupKey).size(); 
} 
+1

résolu mon problème en faisant référence http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/ merci beaucoup pour l'aide :) –