1

I have a weird pronlem with a custom ListFragment into my android app. I have a custom ArrayAdapter for my listView which is handled by a LoaderManager and besides this i also have a headerView attached:

public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    setListAdapter(null);
}

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (getListView().getHeaderViewsCount() <= 0 && headerView != null)

        getListView().addHeaderView(headerView, null, false);

            if (listAdapter == null)
    listAdapter = new DataAdapter(getActivity(), R.id.label1,
            (CategoryModel) getArguments().getParcelable(MODEL_KEY));
    setListAdapter(listAdapter);

    setListShown(false);
    getLoaderManager().initLoader(0, null, this);

     }



    private final static class DataAdapter extends ArrayAdapter<ICatalogModel> {
        private final LayoutInflater inflater;
        private final CategoryModel parentModel;

        private CustomArrayFilter filter;

        public DataAdapter(Context c, int textViewResourceId,
                CategoryModel parentModel) {
            super(c, textViewResourceId);
            this.parentModel = parentModel;
            inflater = LayoutInflater.from(c);
        }

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

        @Override
        public boolean isEnabled(int position) {
            return true;
        }



        public ArrayList<ICatalogModel> getData() {
            return originalValues;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            ItemHolder holder;
            if (v == null) {
                v = inflater.inflate(R.layout.rooms_list_row, parent, false);
                holder = new ItemHolder();
                holder.nameField = (TextView) v.findViewById(R.id.label1);
                v.setTag(holder);
            }

            holder = (ItemHolder) v.getTag();

            final ICatalogModel model = getItem(position);
            holder.nameField.setText(model.getName());
            return v;
        }

        public void addAllSupport(ArrayList<ICatalogModel> data) {
            for (ICatalogModel p : data) {
                add(p);
            }
            notifyDataSetChanged();
        }
}

On the onLoaderReset method i am trying to clear adapter's data by calling myAdapter.clear() but a weird IndexOutOfBounds exception is thrown in the android HeaderViewListAdapter class at line 126:

@Override
public void onLoaderReset(Loader<ArrayList<ICatalogModel>> arg0) {
    if (listAdapter != null) {
            ((DataAdapter) listAdapter).clear();
    }
}

@Override
public void onLoadFinished(Loader<ArrayList<ICatalogModel>> arg0,
        ArrayList<ICatalogModel> arg1) {

    if (listAdapter != null) {
        ((DataAdapter) listAdapter).clear();
        if (CokConstants.SUPPORTS_HONEYCOMB) {
            ((DataAdapter) listAdapter).addAll(arg1);
        } else {
            ((DataAdapter) listAdapter).addAllSupport(arg1);
        }
        ((DataAdapter) listAdapter).setOriginalValues(arg1);
    }
    if (isResumed())
        setListShown(true);
    else
        setListShownNoAnimation(true);
}

I figured it out that the problem was somehow related with the fact that i am not removing the headerView on the onDestroyView method...i did that...no more exception...but now another problem...whenever i go back to that fragment from backstack ...the list is not visible anymore eventhough the data is added correctly in the adapter. Has anyone encountered this problem before ?

mmBs
  • 8,421
  • 6
  • 38
  • 46
  • To help you, we need to see some relevant code. I also recommend formatting your question- right now it is just a wall of text, making it very difficult to read. – Bryan Herbst Nov 15 '13 at 16:51

1 Answers1

0

when you add a header or a footer view to your ListView; the adapter you set to it, is wrapped into WrapperListAdapter. You must check instance of the adapter.

DataAdapter myExistingAdapter = null;
if(listAdapter instanceof WrapperListAdapter) {
    myExistingAdapter = (DataAdapter)((WrapperListAdapter)listAdapter).getWrappedAdapter();
} else if (listAdapter instanceof DataAdapter) {
    myExistingAdapter = (DataAdapter)listAdapter;
}

android.widget.WrapperListAdapter Known Indirect Subclasses HeaderViewListAdapter

Devrim
  • 15,345
  • 4
  • 66
  • 74
  • True...and yes seems the right thing to do now...I was driving in circles around the solution for hours but now seems so damn obvious...calling just clear() on the adapter it will call the clear() method on the WrappedListAdapter in my case and not on the DataAdapter as I needed...thanks man !! – Marius Constantin Nov 17 '13 at 00:22