Attempt to clean up the cursor closed problems by managing the cursor in onResume instead of onAttach.

This commit is contained in:
Kevin Ruland 2012-02-23 17:42:08 +00:00
parent e73527b733
commit 1c5da14cd2

View File

@ -137,17 +137,13 @@ implements SharedPreferences.OnSharedPreferenceChangeListener
public void onViewCreated(View view, Bundle savedInstanceState) { public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState); super.onViewCreated(view, savedInstanceState);
refreshData();
registerForContextMenu(getExpandableListView()); registerForContextMenu(getExpandableListView());
} }
@Override @Override
public void onAttach(Activity activity) { public void onResume() {
super.onAttach(activity);
mDbHelper = new DbAdapter(getActivity());
mDbHelper.open();
Resources resources = this.getResources(); Resources resources = this.getResources();
groupColumnPreferenceKey = resources.getString(R.string.PreferenceMotorBrowserGroupingOption); groupColumnPreferenceKey = resources.getString(R.string.PreferenceMotorBrowserGroupingOption);
@ -157,21 +153,14 @@ implements SharedPreferences.OnSharedPreferenceChangeListener
pref.registerOnSharedPreferenceChangeListener(this); pref.registerOnSharedPreferenceChangeListener(this);
Activity activity = getActivity();
if ( activity instanceof OnMotorSelectedListener ) { if ( activity instanceof OnMotorSelectedListener ) {
motorSelectedListener = (OnMotorSelectedListener) activity; motorSelectedListener = (OnMotorSelectedListener) activity;
} }
Cursor motorCounter = mDbHelper.getMotorDao().fetchAllMotors(); refreshData();
int motorCount = motorCounter.getCount();
motorCounter.close();
if ( motorCount == 0 ) { super.onResume();
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("No Motors Found");
builder.setMessage("Motors can be downloaded from thrustcurve");
builder.setCancelable(true);
builder.create().show();
}
} }
@ -222,8 +211,8 @@ implements SharedPreferences.OnSharedPreferenceChangeListener
} }
@Override @Override
public void onDetach() { public void onPause() {
super.onDetach(); super.onPause();
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getActivity()); SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getActivity());
pref.unregisterOnSharedPreferenceChangeListener(this); pref.unregisterOnSharedPreferenceChangeListener(this);
@ -247,6 +236,23 @@ implements SharedPreferences.OnSharedPreferenceChangeListener
} }
public void refreshData() { public void refreshData() {
if ( mDbHelper == null ) {
mDbHelper = new DbAdapter(getActivity());
}
mDbHelper.open();
Cursor motorCounter = mDbHelper.getMotorDao().fetchAllMotors();
int motorCount = motorCounter.getCount();
motorCounter.close();
if ( motorCount == 0 ) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("No Motors Found");
builder.setMessage("Motors can be downloaded from thrustcurve");
builder.setCancelable(true);
builder.create().show();
}
Cursor motorCursor = mDbHelper.getMotorDao().fetchGroups(groupColumn); Cursor motorCursor = mDbHelper.getMotorDao().fetchGroups(groupColumn);
MotorHierarchicalListAdapter mAdapter = new MotorHierarchicalListAdapter( MotorHierarchicalListAdapter mAdapter = new MotorHierarchicalListAdapter(
getActivity(), getActivity(),