2

我正在尝试为不同组中的孩子实现具有两种不同布局的消耗性列表视图。经过相当多的搜索后,我为我的适配器想出了以下代码。当我运行应用程序并尝试扩展最后一组(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;
    }
}
4

1 回答 1

1

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

于 2012-09-27T16:40:14.403 回答