ealihodzic
ealihodzic

Reputation: 209

How to add different layouts for children in expandable list view in android?

I'm trying to implement expendable list view with two different layouts for children in different groups. After quite a bit of searching I came up with the following code for my adapter. When I run application and try to expand last group (of 4 groups) it crashes with

ArrayIndexOutOfBoundsException: length=2; index=2

no matter for which group I check the condition in getChildType:

 public int getChildType (int groupPosition, int childPosition) {

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

Thanks for any help!

Code for adapter:

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;
    }
}

Upvotes: 2

Views: 1696

Answers (1)

raj
raj

Reputation: 642

Seems the logic for getChildTypeCount(int groupPosition) and getChildType(int groupPosition, int childPosition) is not matching. When group position is not 3 the type is 1 but count is 1 (which means there are more than 1 type)

Upvotes: 1

Related Questions