ArrayIndexOutOfBoundsException with custom Android Adapter for multiple views in ListView

前端 未结 4 1865
暗喜
暗喜 2020-11-28 18:53

I am attempting to create a custom Adapter for my ListView since each item in the list can have a different view (a link, toggle, or radio group), but when I try to run the

4条回答
  •  庸人自扰
    2020-11-28 19:09

    The accepted answer is correct. This is what I am doing to avoid the problem:

    public enum FoodRowType {
        ONLY_ELEM,
        FIRST_ELEM,
        MID_ELEM,
        LAST_ELEM
    }
    
    @Override
    public int getViewTypeCount() {
        return FoodRowType.values().length;
    }
    
    @Override
    public int getItemViewType(int position) {
        return rows.get(position).getViewType();  //returns one of the above types
    }
    

提交回复
热议问题