使用Android

时间:2016-07-29 14:24:11

标签: android expandablelistview custom-adapter expandablelistadapter

我有自定义对象列表,我想用ExpandableListView填充这些对象的数据。但是在自定义适配器上它停留在getGroupCount函数并且它没有错误地停止并且没有传递像getChildrenCount这样的另一个函数。第一个getGroupCount正在getGroupCount之后,但没有其他任何内容。 Expandablelistview未在屏幕上显示。没有显示错误,也没有显示explistview,我找不到问题,我看到我的列表有数据。适配器如下所示,

public class salItemsAdapter extends BaseExpandableListAdapter {

public List<ABSSALEITEM> extSetItems;
public Context context;
public LayoutInflater inflater;
public HashMap<Integer, List<ABSSALEITEM>> extSubSetItems;

public TextView txtSalItemHeaderClm1;
public TextView txtSalItemHeaderClm2;
public TextView txtSalItemHeaderClm3;
public TextView txtSalItemBottomText;


public salItemsAdapter(Context context, List<ABSSALEITEM> extSetItems, HashMap<Integer, List<ABSSALEITEM>> extSubSetItems) {
    this.context = context;
    this.extSetItems = extSetItems;
    this.extSubSetItems = extSubSetItems;
}
@Override
public int getChildrenCount(int groupPosition) {
    return extSubSetItems.get(extSetItems.get(groupPosition)).size();
}

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

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

@Override
public Object getChild(int groupPosition, int childPosition) {
    return extSubSetItems.get(extSetItems.get(groupPosition)).get(childPosition);
}

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

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

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View view, ViewGroup parent)  {
    ABSSALEITEM setSalItem = (ABSSALEITEM)getGroup(groupPosition);

    if(view == null)
    {
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.salitems_elv_head, parent, false);
    }

    txtSalItemHeaderClm1 = (TextView)view.findViewById(R.id.txtSalItemHeaderClm1);
    txtSalItemHeaderClm2 = (TextView)view.findViewById(R.id.txtSalItemHeaderClm2);
    txtSalItemHeaderClm3 = (TextView)view.findViewById(R.id.txtSalItemHeaderClm3);

    txtSalItemHeaderClm1.setText(setSalItem.MAT);
    txtSalItemHeaderClm2.setText(setSalItem.EXPLAIN);
    txtSalItemHeaderClm3.setText(Double.toString(setSalItem.QUANT));

    return view;
}

@Override
public View getChildView(int groupPosition, int childPosition,
                         boolean isLastChild, View view, ViewGroup parent) {
    ABSSALEITEM setSalItem = (ABSSALEITEM)getChild(groupPosition, childPosition);
    if(view==null)
    {
        inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.salitems_elv_item, parent, false);
    }

    txtSalItemBottomText = (TextView)view.findViewById(R.id.txtSalItemBottomText);
    txtSalItemBottomText.setText(setSalItem.LONGEXP);
    return view;
}

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

我填写了像

这样的数据对象
    private void FillExpListView(List<ABSSALEITEM> paSalSetItems, List<ABSSALEITEM> paSalSetSubItems) {

    hbSubSetItems = new HashMap<Integer, List<ABSSALEITEM>>();

    List<ABSSALEITEM> tmp = new ArrayList<ABSSALEITEM>(paSalSetSubItems);
    List<ABSSALEITEM> tmp2 = new ArrayList<ABSSALEITEM>();
    Integer tmpItemnum = 0;

    for (ABSSALEITEM tmpSalItem : tmp) {

        tmpItemnum = tmpSalItem.ITEMNUM;

        if(!hbSubSetItems.containsKey(tmpItemnum))
        {
            tmp2.clear();
            for (ABSSALEITEM tmpSalItem2 : paSalSetSubItems) {
                if (tmpItemnum == tmpSalItem2.ITEMNUM) {
                    tmp2.add(tmpSalItem2);
                }
            }
            hbSubSetItems.put(tmpSalItem.ITEMNUM, tmp2);
        }
    }

    adapterSalItems = new salItemsAdapter(salitems.this, paSalSetItems, hbSubSetItems);
    elvSalItems.setAdapter(adapterSalItems);
}

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您可以使用GitHub上提供的库。创建两个布局: - 一个用于标题(用户单击并展开的项目),另一个用于单击标题时可见的项目。

有些图书馆

enter image description here

enter image description here

enter image description here