上下文 我有一个drawerLayout,其中包含我从Realm获得的城市列表。 ViewPager已经显示该列表。您可以直接从DrawerLayout
删除城市 问题:当我从DrawerLayout中删除一个城市时,我调用mViewPager.getAdapter()。notifyDataSetChanged,不重新加载viewPager。由于drawerLayout显示在ViewPager上,因此当您关闭它时没有重新加载,当我导航回ViewPager / Realm时,ViewPager / Realm返回java.lang.ArrayIndexOutOfBoundsException: rowIndex > available rows: 4 > 4
。
你对我如何解决这个问题有任何线索吗?
出于提供信息的目的,以下是它的工作原理: **在BaseActivity中,包含DrawerLayout **
mCityListAdapter = new CityListAdapter(getBaseContext(), RealmHelper.getStoredCities(), new CityListAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
mPager.setCurrentItem(position);
mDrawerLayout.closeDrawers();
}
}, new CityListAdapter.OnItemLongClickListener() {
@Override
public void onItemLongClick(final int position) {
final CharSequence[] items = {getString(R.string.action_delete_city)};
AlertDialog.Builder builder = new AlertDialog.Builder(BaseActivity.this);
builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
RealmHelper.removeCity(RealmHelper.getStoredCities().get(position));
mPager.getAdapter().notifyDataSetChanged();
}
});
builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
builder.setTitle(getString(R.string.delete_city_label));
builder.show();
}
});
在包含viewPager
的活动中@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLayoutInflater().inflate(R.layout.fragment_pager, mFrameLayout);
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mPager.addOnPageChangeListener(this);
mAdapter = new FragmentStatePageSupportAdapter(getSupportFragmentManager(), RealmHelper.getStoredCities().size(), this, RealmHelper.getStoredCities());
mPager.setAdapter(mAdapter);
mPager.setOffscreenPageLimit(0);
}
修改
我确实覆盖了getItemPosition,并且它被调用了。所以不是问题。
The @Override
public int getItemPosition(Object object) {
Log.v("results", "called getitempostion");
return POSITION_NONE;
}
答案 0 :(得分:1)
只有在实际发生变更时才应通知数据集,如此
mRealmChangeListener = new RealmChangeListener() {
@Override
public void onChange(Object element) {
mCityListAdapter.notifyDataSetChanged();
}
}
RealmHelper.getStoredCities().addChangeListener(mRealmChangeListener);
并且
mCityListAdapter = new CityListAdapter(getBaseContext(), RealmHelper.getStoredCities(), new CityListAdapter.OnItemClickListener() {
@Override
public void onItemClick(int position) {
...
}
}, new CityListAdapter.OnItemLongClickListener() {
@Override
public void onItemLongClick(final int position) {
...
AlertDialog.Builder builder = new AlertDialog.Builder(BaseActivity.this);
builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
RealmHelper.removeCity(RealmHelper.getStoredCities().get(position));
////mPager.getAdapter().notifyDataSetChanged(); // remove this
}
});
...
}
});
编辑:好吧,我想在那种情况下你应该走更长的路线:
mRealmChangeListener = new RealmChangeListener() {
@Override
public void onChange(Object element) {
mCityListAdapter.notifyDataSetChanged();
}
}
RealmResults<City> listenerSet;
Realm realm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
realm = Realm.getDefaultInstance();
listenerSet = realm.where(City.class).findAll();
listenerSet.addChangeListener(mRealmChangeListener);
}
或类似的东西。