我的Android应用程序中有一个带有自定义ListFragment
的怪异内容。我有ArrayAdapter
的自定义listView
由LoaderManager
处理,除此之外我还附加了headerView
:
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();
}
}
在onLoaderReset
方法上,我试图通过调用myAdapter.clear()
来清除适配器的数据,但是在第126行的android IndexOutOfBounds
类中抛出了一个奇怪的HeaderViewListAdapter
异常:< / p>
@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);
}
我认为这个问题在某种程度上与我在headerView
方法中删除onDestroyView
这一事实有关...我做到了......没有例外......但现在又出现了另一个问题......每当我从backstack回到那个片段时......只要数据在适配器中正确添加,该列表就不再可见了。
有没有人遇到过这个问题?
答案 0 :(得分:0)
向ListView
添加页眉或页脚视图时;您设置的适配器被包装到WrapperListAdapter
中。您必须检查适配器的实例。
DataAdapter myExistingAdapter = null;
if(listAdapter instanceof WrapperListAdapter) {
myExistingAdapter = (DataAdapter)((WrapperListAdapter)listAdapter).getWrappedAdapter();
} else if (listAdapter instanceof DataAdapter) {
myExistingAdapter = (DataAdapter)listAdapter;
}
android.widget.WrapperListAdapter 已知的间接子类 HeaderViewListAdapter