如何在android中的可扩展列表视图中为子项添加不同的布局?

时间:2012-07-29 20:41:59

标签: list layout view children expandable

我正在尝试为不同组中的孩子实现具有两种不同布局的消耗性列表视图。经过相当多的搜索后,我想出了适配器的以下代码。当我运行应用程序并尝试扩展最后一组(4组)时,它会崩溃

ArrayIndexOutOfBoundsException: length=2; index=2

无论我在哪个组中检查getChildType中的条件:

 public int getChildType (int groupPosition, int childPosition) {

    Log.i(TAG, "getChildType("+groupPosition+","+childPosition+") called");
    if (groupPosition == 3)
        return 1;
    else return 0;
    }

感谢您的帮助!

适配器代码:

public class ExpListAdapter extends BaseExpandableListAdapter {

    private String TAG = "ExpListAdapter";

    private String arrGroupelements[];
    private String arrChildelements[][][];

    private Context myContext;
    public ExpListAdapter(Context context, String[] groupElements, String[][][] childElements) {
    myContext = context;
    arrGroupelements = groupElements;
    arrChildelements = childElements;

    }
    public Object getChild(int groupPosition, int childPosition) {
    return arrChildelements[groupPosition][childPosition];
    }

    public int getChildType (int groupPosition, int childPosition) {

    Log.i(TAG, "getChildType("+groupPosition+","+childPosition+") called");
    if (groupPosition == 3)
        return 1;
    else return 0;
    }

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

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

    LayoutInflater inflater =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    int gp = groupPosition;
    int cp = childPosition;

    Log.d(TAG, "groupPosition: "+gp);
    Log.d(TAG, "childPosition: "+cp);
    Log.d(TAG, "isLastChild: "+isLastChild);

    if (convertView == null) {
        if (getChildType(groupPosition,childPosition)==0)
        convertView = inflater.inflate(R.layout.child_row, null);
        else
        convertView = inflater.inflate(R.layout.child_row_slide_btn, null);

    }
    if (getChildType(groupPosition,childPosition)==0) {
        TextView fieldInfo = (TextView) convertView.findViewById(R.id.fieldInfo);
        fieldInfo.setText(arrChildelements[gp][cp][0]);

        EditText editField = (EditText) convertView.findViewById(R.id.editField);

        editField.setText(arrChildelements[gp][cp][1], null);

        editField.addTextChangedListener( new TextWatcher() {

        public void afterTextChanged(Editable s) { 
            //                  Log.i(TAG,"afterTextChanged called");
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {  }

        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }
        });
    } else {
        CheckBox cb = (CheckBox)convertView.findViewById(R.id.onOff);
        cb.setText("konj");
    }

    return convertView;
    }

    public int  getChildTypeCount (int groupPosition) {
    if (groupPosition==3)
    return 2;
else 
    return 1;
    }

    public int getChildrenCount(int groupPosition) {
    return arrChildelements[groupPosition].length;
    }

    public Object getGroup(int groupPosition) {
    return arrGroupelements[groupPosition];
    }

    public int getGroupCount() {
    return arrGroupelements.length;
    }

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

    public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {

    if (convertView == null) {
        LayoutInflater inflater =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.group_row, null);
    }

    TextView groupTitle = (TextView) convertView.findViewById(R.id.groupTitle);
    groupTitle.setText(arrGroupelements[groupPosition]);

    return convertView;
    }

    public boolean hasStableIds() {
    return true;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
    }
}

1 个答案:

答案 0 :(得分:1)

似乎getChildTypeCount(int groupPosition)getChildType(int groupPosition, int childPosition)的逻辑不匹配。 当组位置 3时,类型为1但计数为1(这意味着有多于1种类型)