在Expandable listview的子视图中获取错误

时间:2015-08-20 11:57:22

标签: android arraylist android-arrayadapter expandablelistview

我想在我的应用程序中使用ExpandableListView。直到现在一切正常,除了childView。所有的childView都与每个单独的parentView匹配。

public class AdTypesAdapter extends BaseExpandableListAdapter {

Context context;
public LayoutInflater minflater;
ArrayList<Q2_AdTypeList> adTypeList;

public AdTypesAdapter(Context context, ArrayList<Q2_AdTypeList> adTypeList) {
    // TODO Auto-generated constructor stub
    this.adTypeList = adTypeList;
    context = context;
}

public void setInflater(LayoutInflater mInflater, Context context) {
    this.minflater = mInflater;
    context = context;
}

@Override
public int getGroupCount() {
    // TODO Auto-generated method stub
    return adTypeList.size();
}

@Override
public int getChildrenCount(int groupPosition) {
    // TODO Auto-generated method stub
    return adTypeList.size();
}

@Override
public Object getGroup(int groupPosition) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return adTypeList.get(childPosition);
}

@Override
public long getGroupId(int groupPosition) {
    // TODO Auto-generated method stub
    return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return childPosition;
}

@Override
public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return false;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = minflater.inflate(R.layout.ad_details_parent_layout,
                null);
    }

    TextView noOfImpressions_textView = (TextView) convertView
            .findViewById(R.id.noOfImpressions_textView);
    TextView amount_textView = (TextView) convertView
            .findViewById(R.id.amount_textView);
    noOfImpressions_textView.setText(String.valueOf(adTypeList.get(
            groupPosition).getAdvImpression()));
    amount_textView.setText("$"
            + String.valueOf(adTypeList.get(groupPosition).getAdvPrice()));

    // ((TextView) convertView).setChecked(isExpanded);
    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {

    TextView adDesc_textView = null;
    if (convertView == null) {
        convertView = minflater.inflate(R.layout.ad_details_child_layout,
                null);
    }
    adDesc_textView = (TextView) convertView
            .findViewById(R.id.adDesc_textView);

    adDesc_textView.setText(adTypeList.get(childPosition).getAdvDesc());
    return convertView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return false;
}

}

此处,adTypeList包含父视图和子视图的数据![ I would like to get exactly what it is in the image. But what I get in my output is that the total number of childview I have is attached to each and every parentview] 1

1 个答案:

答案 0 :(得分:1)

我认为你的getChildrenCount应该总是根据你的数据模型返回1(每个父母只有一个孩子)

@Override
public int getChildrenCount(int groupPosition)
{
    return 1;
}

通过返回adTypeList.size(),您基本上可以说位置groupPosition的小组有adTypeList.size()个孩子。