在列表中向上滚动时,ArrayAdapter会引发异常

时间:2011-09-08 08:44:43

标签: java android logcat

我使用自定义适配器来创建列表,通常它工作正常,但每当我向下滚动并备份时,一旦标题显示/加载(我猜后者)它就会崩溃。它按预期工作,我可以在列表中滚动,只要我愿意,但是一旦我回到列表的最顶层,就会抛出IllegalStateException。 LogCat输出以下行:

  

ERROR / ArrayAdapter(287):您必须为TextView提供资源ID

我正在使用的列表适配器的代码:

public class SeparatedListAdapter extends BaseAdapter implements ObservableAdapter{

    public final Map<String, Adapter> sections = new LinkedHashMap<String, Adapter>();
    public final ArrayAdapter<String> headers;
    public final static int TYPE_SECTION_HEADER = 0;

    public SeparatedListAdapter(Context context, int resId) {
        super();
        headers = new ArrayAdapter<String>(context, resId);
    }

    public void addSection(String section, Adapter adapter) {
        this.headers.add(section);
        this.sections.put(section, adapter);
        adapter.registerDataSetObserver(mDataSetObserver);
    }

    public void removeObserver() {
        for (Map.Entry<String, Adapter> it : sections.entrySet()) {
            if (it.getValue() instanceof ObservableAdapter) {
                ObservableAdapter adapter = (ObservableAdapter)it.getValue();
                adapter.removeObserver();
            }
        }
    }

    public void clear() {
        headers.clear();
        sections.clear();
        notifyDataSetInvalidated();
    }

    @Override
    public Object getItem(int position) {
        for (Object section : this.sections.keySet()) {
            Adapter adapter = sections.get(section);
            int size = adapter.getCount() + 1;

            if (position == 0) return section;
            if (position < size) return adapter.getItem(position - 1);
            position -= size;
        }
        return null;
    }

    @Override
    public int getCount() {
        int total = 0;
        for (Adapter adapter : this.sections.values())
            total += adapter.getCount() + 1;
        return total;
    }

    @Override
    public int getViewTypeCount() {
        int total = 1;
        for (Adapter adapter : this.sections.values())
            total += adapter.getViewTypeCount();
        return total;
    }

    @Override
    public int getItemViewType(int position) {
        int type = 1;
        for (Object section : this.sections.keySet()) {
            Adapter adapter = sections.get(section);
            int size = adapter.getCount() + 1;

            if (position == 0) return TYPE_SECTION_HEADER;
            if (position < size) return type + adapter.getItemViewType(position - 1);

            position -= size;
            type += adapter.getViewTypeCount();
        }
        return -1;
    }


    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        return (getItemViewType(position) != TYPE_SECTION_HEADER);
    }

    @Override
    public boolean isEmpty() {
        return getCount() == 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int sectionnum = 0;
        for (Object section : this.sections.keySet()) {
            Adapter adapter = sections.get(section);
            int size = adapter.getCount() + 1;

            if (position == 0) return headers.getView(sectionnum, convertView, parent);
            if (position < size) return adapter.getView(position - 1, convertView, parent);

            position -= size;
            sectionnum++;
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }    

    private DataSetObserver mDataSetObserver = new DataSetObserver() {
        @Override
        public void onChanged() {
            notifyDataSetChanged();   
        }
    };

}

我很失落,如果有人有任何想法,我真的很感激。

1 个答案:

答案 0 :(得分:0)

使用以下类型的代码

if (position == 0) return ...;
if (position < size) return ...;
position -= size;

position是&gt; = 2时,size有可能变为负值而不会被设置为0。我会检查是否是这种情况。