在 Android 中使用自定义列表对象填充可扩展列表视图

Populate expandable listview with custom list objects in Android

我有自定义对象列表,我想用这些对象的数据填充 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);
}

感谢帮助

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

有些图书馆