0

I am using a custom adapter to create a list, generally it works fine, but whenever I scroll down a bit and back up, it will crash once the headline is supposed to be shown / loaded (I am guessing the latter). It works as intended and I can scroll through the list for as long as I like, but once I back up to the very top of the list, an IllegalStateException is thrown. LogCat outputs the following line:

ERROR/ArrayAdapter(287): You must supply a resource ID for a TextView

The code for the list adapter I am using:

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

}

I'm quite lost, if anyone has any ideas, I'd really appreciate it.

Tim
  • 1

1 Answers1

0

With the following type of code

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

There is a danger of position becoming minus without ever being set to 0, when the size is >= 2. I would check if this is the case.

Caner
  • 57,267
  • 35
  • 174
  • 180