| Nom de la méthode | Aperçu | 
|---|---|
| startDelete(int token, Object cookie, Uri uri, String selection, String[] selectionArgs) | Supprimer de manière asynchrone. | 
| startInsert(int token, Object cookie, Uri uri, ContentValues initialValues) | Insérer de manière asynchrone. | 
| startQuery(int token, Object cookie, Uri uri, String[] projection, String selection, String[] selectionArgs, String orderBy) | Requête de manière asynchrone. | 
| startUpdate(int token, Object cookie, Uri uri, ContentValues values, String selection, String[] selectionArgs) | Mettre à jour de manière asynchrone. | 
| onDeleteComplete(int token, Object cookie, int result) | Appelé lorsque la méthode startDelete se termine | 
| onInsertComplete(int token, Object cookie, Uri uri) | Appelé lorsque la méthode startInsert se termine | 
| onQueryComplete(int token, Object cookie, Cursor cursor) | Appelé lorsque la méthode startQuery se termine | 
| onUpdateComplete(int token, Object cookie, int result) | Appelé lorsque la méthode startUpdate se termine | 
Le premier argument, token, est la valeur utilisée pour identifier le processus. Le deuxième argument cookie est l'objet à transmettre une fois terminé Le troisième argument de la méthode appelée à la fin est la valeur de retour de chaque méthode correspondante
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();
    }
}
Il y a peut-être une meilleure solution, mais c'était la solution à l'époque.
https://developer.android.com/reference/android/content/AsyncQueryHandler.html http://shop.oreilly.com/product/0636920029397.do
Recommended Posts