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 ?