I am using ActionbarSherlock with a SherlockListFragment that implements LoaderManager.LoaderCallbacks.
In my ApplicationActivity onCreate method I am using
setContentView(R.layout.application);
to set the layout -- works great.
I am initializing the actionbar like so
ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
bar.setDisplayHomeAsUpEnabled(false);
bar.setDisplayShowTitleEnabled(true);
// users event list
bar.addTab(bar.newTab()
.setTag("event_list")
.setText(getString(R.string.list_events_header))
.setTabListener(new TabListener<EventListFragment>(
this, getString(R.string.list_events_header), EventListFragment.class, null)));
Within the ApplicationActivity, I have an AsyncTask that takes a couple of seconds to load on initial open, and when manually refreshed against the API - which means that I need to make sure I update the ListView on the fragment instantiated above, which I do in the onPostExecute method, here is how I do that:
// update the events fragment
EventListFragment fragment = (EventListFragment) getSupportFragmentManager().findFragmentById(R.string.list_events_header);
if(fragment != null) {
// restart the loader for this fragment, refreshing the listview
getSupportLoaderManager().restartLoader(0, null, fragment);
}
THIS ALL WORKS
Now, I wanted to put in a TabsAdapter to get the fancy swiping tabs, which I've done, and it works, but the last part I mentioned about the onPostExecute, doesnt work :(
This is my TabsAdapter:
package com.lateral.app.ui;
import java.util.ArrayList;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
public class APPTabsAdapter extends FragmentPagerAdapter implements
ActionBar.TabListener, ViewPager.OnPageChangeListener {
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
static final class TabInfo {
private final Class<?> clss;
private final Bundle args;
TabInfo(Class<?> _class, Bundle _args) {
clss = _class;
args = _args;
}
}
public APPTabsAdapter(ApplicationActivity activity, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mActionBar = activity.getSupportActionBar();
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public APPTabsAdapter(EventActivity activity, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mActionBar = activity.getSupportActionBar();
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
tab.setTabListener(this);
mTabs.add(info);
mActionBar.addTab(tab);
notifyDataSetChanged();
}
@Override
public int getCount() {
return mTabs.size();
}
@Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
}
public void onPageScrollStateChanged(int state) {
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Object tag = tab.getTag();
for (int i = 0; i < mTabs.size(); i++) {
if (mTabs.get(i) == tag) {
mViewPager.setCurrentItem(i);
}
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
This is my updated onCreate method from the ApplicationActivity
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.pager);
setContentView(mViewPager);
ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
bar.setDisplayHomeAsUpEnabled(false);
bar.setDisplayShowTitleEnabled(true);
mTabsAdapter = new APPTabsAdapter(this, mViewPager);
// users event list
mTabsAdapter.addTab(bar.newTab()
.setTag("event_list")
.setText(getString(R.string.list_events_header))
.setTabListener(new TabListener<EventListFragment>(
this, getString(R.string.list_events_header), EventListFragment.class, null)), EventListFragment.class, null);
And this is the update I made to my onPostExecute method in the ApplicationActivity
// update the events fragment
EventListFragment fragment = (EventListFragment) mTabsAdapter.getItem(0);
if(fragment != null) {
// restart the loader for this fragment, refreshing the listview
getSupportLoaderManager().restartLoader(0, null, fragment);
}
Basically, when it attempts to run my onPostExecute code
I get a NullPointerException from my cursorAdapter within the fragment.. but it found the fragment..
EDIT -- Requested Code
public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) {
Log.d(TAG, "onCreateLoader");
return new EventCursorLoader(getActivity());
}
public static final class EventCursorLoader extends SimpleCursorLoader {
Context mContext;
public EventCursorLoader(Context context) {
super(context);
Log.d("EventCursorLoader", "Constructor");
mContext = context;
}
@Override
public Cursor loadInBackground() {
Log.d("EventCursorLoader", "loadInBackground");
EventsDataSource datasource = new EventsDataSource(mContext, ((ApplicationActivity)mContext).getDbHelper());
SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
long userId = app_preferences.getLong("authenticated_user_id", 0);
return datasource.getAllEvents(userId);
}
}