Skip to content

Commit

Permalink
Catch changes to the ListAdapter's data sets
Browse files Browse the repository at this point in the history
Despite the CursorAdapter documentation, onContentChanged doesn't
appear to have any requery logic by default.

And CursorTreeAdapter doesn't offer to do any such action, or provide
an interface of the same kind as onContentChanged.

So simply replace the adapter whenever the dataset changes. This keeps
the cursor refresh off the UI thread and lets the progress spinner
appear when refreshing.

This is closer to the current recommendation in Cursor.requery()'s
documentation, which is to instead recreate the Cursor in a background
thread.

Note that we can't replace the SimpleCursor*Adapter's Cursor here as
that will trigger the DataSetObserver again, leading to an infinite
loop.
  • Loading branch information
TBBle committed Oct 23, 2011
1 parent 1b32589 commit 0dda30c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 22 deletions.
38 changes: 32 additions & 6 deletions MLPFIM/src/com/bubblesworth/soundboard/mlpfim/AboutActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.graphics.Typeface;
import android.media.AudioManager;
import android.media.MediaPlayer;
Expand Down Expand Up @@ -63,9 +64,24 @@ public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
}
}

private ProgressDialog spinner;
private GetCreditsCursor taskHolder;

private class GetCreditsCursor extends AsyncTask<Void, Void, Cursor> {
private ProgressDialog spinner;

@Override
protected void onPreExecute() {
super.onPreExecute();
if (taskHolder != null)
taskHolder.cancel(false);
taskHolder = this;
Resources resources = getResources();
spinner = ProgressDialog.show(AboutActivity.this,
resources.getString(R.string.dialog_loading_title),
resources.getString(R.string.dialog_loading_message), true,
false);
}

protected Cursor doInBackground(Void... voids) {
String[] columns = { CreditColumns._ID, CreditColumns.CREDIT_NAME,
CreditColumns.CREDIT_LINK };
Expand All @@ -83,6 +99,21 @@ protected void onPostExecute(Cursor result) {

adapter.setViewBinder(new AboutViewBinder());
setListAdapter(adapter);

adapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
new GetCreditsCursor().execute();
}
});
taskHolder = null;
}

@Override
protected void onCancelled() {
spinner.dismiss();
taskHolder = null;
super.onCancelled();
}
}

Expand All @@ -94,11 +125,6 @@ protected void onPostExecute(Cursor result) {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Resources resources = getResources();
spinner = ProgressDialog.show(this,
resources.getString(R.string.dialog_loading_title),
resources.getString(R.string.dialog_loading_message), true,
false);
new GetCreditsCursor().execute();
onWindowFocusChanged(hasWindowFocus());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.media.AudioManager;
import android.media.RingtoneManager;
import android.net.Uri;
Expand Down Expand Up @@ -65,9 +66,24 @@ protected Cursor getChildrenCursor(Cursor arg0) {
}
};

private ProgressDialog spinner;
private GetCategoryCursor taskHolder;

private class GetCategoryCursor extends AsyncTask<Void, Void, Cursor> {
private ProgressDialog spinner;

@Override
protected void onPreExecute() {
super.onPreExecute();
if (taskHolder != null)
taskHolder.cancel(false);
taskHolder = this;
Resources resources = getResources();
spinner = ProgressDialog.show(SoundChooserActivity.this,
resources.getString(R.string.dialog_loading_title),
resources.getString(R.string.dialog_loading_message), true,
false);
}

protected Cursor doInBackground(Void... voids) {
String[] columns = { SoundColumns._ID, SoundColumns.DESCRIPTION,
SoundColumns.ICON };
Expand All @@ -79,31 +95,40 @@ protected void onPostExecute(Cursor result) {
spinner.dismiss();
if (result.getCount() == 0) {
showDialog(DIALOG_NO_CONTENT);
} else {
SimpleCursorTreeAdapter adapter = new MySimpleCursorTreeAdapter(
SoundChooserActivity.this, result,
R.layout.icon_expandable_list_item, new String[] {
SoundColumns.DESCRIPTION, SoundColumns.ICON },
new int[] { R.id.listText, R.id.listIcon },
R.layout.icon_list_item, new String[] {
SoundColumns.DESCRIPTION, SoundColumns.ICON },
new int[] { R.id.listText, R.id.listIcon });
setListAdapter(adapter);
}
SimpleCursorTreeAdapter adapter = new MySimpleCursorTreeAdapter(
SoundChooserActivity.this, result,
R.layout.icon_expandable_list_item, new String[] {
SoundColumns.DESCRIPTION, SoundColumns.ICON },
new int[] { R.id.listText, R.id.listIcon },
R.layout.icon_list_item, new String[] {
SoundColumns.DESCRIPTION, SoundColumns.ICON },
new int[] { R.id.listText, R.id.listIcon });
setListAdapter(adapter);

adapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
new GetCategoryCursor().execute();
}
});
taskHolder = null;
}

@Override
protected void onCancelled() {
spinner.dismiss();
taskHolder = null;
super.onCancelled();
}
}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Resources resources = getResources();
widgetConfig = getIntent().getAction().equals(
"com.bubblesworth.soundboard.APPWIDGET_CONFIGURE");
spinner = ProgressDialog.show(this,
resources.getString(R.string.dialog_loading_title),
resources.getString(R.string.dialog_loading_message), true,
false);
new GetCategoryCursor().execute();
if (widgetConfig) {
setResult(RESULT_CANCELED);
Expand Down

0 comments on commit 0dda30c

Please sign in to comment.