在 ExpandableListView 中创建了两次子项 Android

Child item created two times in ExpandableListView Android

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;
}
}

以上是我的代码。 在每个组项下创建两次子项。 在我的组和子列表中,有两个元素。实际上我上面代码的预期结果是:

Group1 child1 Group2 child2

我找不到错误。请任何人帮助我。

将此 getChild 方法替换为:

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

正确一个:

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

还有,

 @Override
    public int getChildrenCount(int groupPosition) {
        return child_data.get(group_data.get(groupPosition))
                .size();
    }
public ExpandableListAdapters(Context context, List<String> listDataHeader,
                             HashMap<String, List<String>> listChildData) {
    this._context = context;
    Log.e("HEADER SIZE", "" + listDataHeader.size() + "..." + listChildData.size());

}

字段 group_datachild_data 都是 List<String> 类型,因此您不知道每个组实际可用的子元素数量。您应该使用构造函数中声明的类型。 List<String> group_data;HashMap<String, List<String>> child_data;`

HashMap<String, List<String>>: 第一个参数 String 代表您的组元素。 第二个参数 List<String> 包含该组的子元素。

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

在此方法中,您必须告诉 ExandableListView,有多少子元素可用于 groupPosition 的组。因此,您必须在 groupPosition :

处指定可用于该组的子元素数量
@Override
public int getChildrenCount(int groupPosition) {
    return child_data.get(group_data.get(groupPosition)).size();
}

您的代码如下。在这里,您从 child_data 中获取数据,其中包含组位置索引。所以对于一个组,所有 child 具有相同的组位置,

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

所以代码改为child位置

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

它会帮助你。

如果它不适合你

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

取第一个构造函数

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

}

并替换第一个代码

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

并且还替换了函数

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