Method name | Overview |
---|---|
startDelete(int token, Object cookie, Uri uri, String selection, String[] selectionArgs) | Delete asynchronously. |
startInsert(int token, Object cookie, Uri uri, ContentValues initialValues) | Insert asynchronously. |
startQuery(int token, Object cookie, Uri uri, String[] projection, String selection, String[] selectionArgs, String orderBy) | Query asynchronously. |
startUpdate(int token, Object cookie, Uri uri, ContentValues values, String selection, String[] selectionArgs) | Update asynchronously. |
onDeleteComplete(int token, Object cookie, int result) | Called when the startDelete method completes |
onInsertComplete(int token, Object cookie, Uri uri) | Called when the startInsert method completes |
onQueryComplete(int token, Object cookie, Cursor cursor) | Called when the startQuery method completes |
onUpdateComplete(int token, Object cookie, int result) | Called when the startUpdate method completes |
The token of the first argument is the value used to identify the process. The second argument cookie is the object to pass when completed The third argument of the method called at completion is the return value of each corresponding method
class CustomAsyncQueryHandler extends AsyncQueryHandler {
private static final String[] PROJECTION = new String[]{UserColumns.ID, UserColumns.NAME, UserColumns.EMAIL};
private final AsyncQueryHandlerActivity activity;
CustomAsyncQueryHandler(ContentResolver cr, AsyncQueryHandlerActivity activity) {
super(cr);
this.activity = activity;
}
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
super.onQueryComplete(token, cookie, cursor);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
activity,
android.R.layout.simple_list_item_2,
cursor,
PROJECTION,
new int[]{android.R.id.text1, android.R.id.text2});
activity.setListAdapter(adapter);
}
void insert(String name, String address) {
ContentValues value = new ContentValues();
value.put(UserColumns.NAME, name);
value.put(UserColumns.EMAIL, address);
startInsert(0, null, UserColumns.CONTENT_URI, value);
}
void delete() {
startDelete(0, null, UserColumns.CONTENT_URI, null, null);
}
void findAll() {
startQuery(0, null, UserColumns.CONTENT_URI, PROJECTION, null, null, null);
}
void findById(String where, String[] value) {
startQuery(0, null, UserColumns.CONTENT_URI, PROJECTION, where, value, null);
}
}
Activity
public class HogeHogeActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CustomAsyncQueryHandler asyncQueryHandler = new CustomAsyncQueryHandler(getContentResolver(), this);
// Delete
asyncQueryHandler.delete();
// Insert
for (int i = 1; i <= 50; i++) {
asyncQueryHandler.insert(UserColumns.NAME + i, UserColumns.EMAIL + i);
}
// Select
asyncQueryHandler.findAll();
}
}
Perhaps there is a better way, but this was the solution at the time.
https://developer.android.com/reference/android/content/AsyncQueryHandler.html http://shop.oreilly.com/product/0636920029397.do
Recommended Posts