first I want to say that I'm really new to Android developing and programming at all, and I try to do things that I don't fully understand, because, how then learn if not like this? (like using CursorLoader instead of deprecated startManagingCursor). Second that I'm using custom library click.
It's awesome and easy in use. Now I'll try to describe my issue.
I have 3 separate fragments added in my CustomPagerAdapter.
private static class CustomPagerAdapter extends FragmentPagerAdapter
implements SlidingTabLayoutColor.ColorProvider, SlidingTabLayoutColor.ImageProvider {
ArrayList<Fragment> fragments;
public CustomPagerAdapter(FragmentManager fm) {
super(fm);
fragments = new ArrayList<Fragment>();
fragments.add(new FragmentAll());
fragments.add(new FragmentFav());
fragments.add(new FragmentNotFav());
}
}
In each fragment I have ListView with records from database where I can delete them with contextmenu (adding records making via separate Activity).
public boolean onContextItemSelected(MenuItem item) {
if (item.getItemId() == 1) {
AdapterView.AdapterContextMenuInfo acmi = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
db.delRec(acmi.id);
getLoaderManager().getLoader(0).forceLoad();
return true;
}
return super.onContextItemSelected(item);
}
The issue is when I'm deleting record from one Fragment and swiping to next Fragment, the second Fragment wouldn't update ListView and will show old record that already have been deleted. Lifecycle don't helps me in that case, because there nothing happens when you swiping two near standing fragments. The author of the library adviced me to Override the onPageSelected method, but I don't understand how to update the ListView that is in Fragment from the Activity. I read a lot of same problem on stackoverflow, but didn't find suitable solution that works with CursorLoader. In each Fragment's onCreateView I past this:
getLoaderManager().initLoader(0, null, this);
Also I Override these methods and creating MyCursorLoader class:
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle bundle) {
return new MyCursorLoader(getActivity(), db);
}
@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
scAdapter.swapCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> cursorLoader) {
}
static class MyCursorLoader extends CursorLoader {
DB db;
public MyCursorLoader(Context context, DB db) {
super(context);
this.db = db;
}
@Override
public Cursor loadInBackground() {
Cursor cursor = db.getAllData();
return cursor;
}
}
I tried to make method in Fragment like this:
public void refreshData() {
getLoaderManager().getLoader(0).forceLoad();
}
And call it from onPageSelected:
@Override
public void onPageSelected(int i) {
FragmentAll fragmentAll = new FragmentAll();
if (i == 0) {
fragmentAll.refreshData();
}
}
But this caused an error. Please help me with my problem, and don't forget that I'm beginner. ;) Thank you for your attention!
I think the problem lies here:
@Override
public void onPageSelected(int i) {
FragmentAll fragmentAll = new FragmentAll();
if (i == 0) {
fragmentAll.refreshData();
}
}
You are creating a new instance of FragmentAll
every time page at position 0 is selected, instead of calling refreshData
on the original fragment that you added in your CustomPagerAdapter
.
So what you should be doing here is to get the instance of the original fragments and call refreshData
on them whenever the data is updated i.e. inside OnContextItemSelected
. You can do this my creating a method respond
in your Activity
that hosts the ViewPager
:
public void respond() {
((FragmentAll) customPagerAdapter.fragments.get(0)).refreshData();
((FragmentFav) customPagerAdapter.fragments.get(1)).refreshData();
((FragmentNotFav) customPagerAdapter.fragments.get(2)).refreshData();
}
And this is what your OnContextItemSelected
should look like:
public boolean onContextItemSelected(MenuItem item) {
if (item.getItemId() == 1) {
AdapterView.AdapterContextMenuInfo acmi = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
db.delRec(acmi.id);
//getLoaderManager().getLoader(0).forceLoad();
((NameOfYourActivityThatHostsViewPager) getActivity()).respond();
return true;
}
return super.onContextItemSelected(item);
}
Note that this requires you to have refreshData
method in all of your fragments (All, Fav and NotFav).
I hope this resolves your query.