【问题标题】:Child item created two times in ExpandableListView Android在 ExpandableListView Android 中创建了两次子项
【发布时间】:2016-10-17 12:06:34
【问题描述】:
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;
}
}

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

组 1 子 1 组 2 子 2

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

【问题讨论】:

    标签: android expandablelistview repeat expandablelistadapter


    【解决方案1】:

    将此 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();
        }
    

    【讨论】:

    【解决方案2】:
    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&lt;String&gt;,因此您不知道每个组实际上有多少子元素可用。您应该使用构造函数中声明的类型。 List&lt;String&gt; group_data;HashMap&lt;String, List&lt;String&gt;&gt; child_data;`

    HashMap&lt;String, List&lt;String&gt;&gt;: 第一个参数String 代表您的组元素。 第二个参数List&lt;String&gt; 包含该组的子元素。

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

    在这种方法中,您必须告诉ExandableListViewgroupPosition 的组有多少子元素可用。因此,您必须在 groupPosition 处指定该组有多少子元素可用:

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

    【讨论】:

    【解决方案3】:

    您的代码如下。在此,您从 child_data 获取具有组位置索引的数据。所以对于一个组,所有的孩子都有相同的组位置,

        @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(childPosititon);
    }
    

    它会帮助你。

    【讨论】:

    • 我已将 get(groupPostition) 更改为 get(childPosition)。但没有变化
    • 嗨,这个构造函数是什么意思? public ExpandableListAdapters(上下文上下文,List listDataHeader,HashMap> listChildData){ this._context = context; Log.e("标题大小", "" + listDataHeader.size() + "..." + listChildData.size()); }
    • 通过引用 androidhive.info/2013/07/android-expandable-list-view-tutorial 解决了我的问题,非常感谢您的帮助 :)
    • 好的daa。这不是传递数据的正确方式。放置一个设置器并将其分配给全局变量。并调用 notifydatasetchanged() 方法
    【解决方案4】:

    如果它不适合你

    @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();
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-15
    • 1970-01-01
    • 1970-01-01
    • 2015-05-24
    • 1970-01-01
    • 2017-10-21
    相关资源
    最近更新 更多