Example usage for android.content AsyncQueryHandler startQuery

List of usage examples for android.content AsyncQueryHandler startQuery

Introduction

In this page you can find the example usage for android.content AsyncQueryHandler startQuery.

Prototype

public void startQuery(int token, Object cookie, Uri uri, String[] projection, String selection,
        String[] selectionArgs, String orderBy) 

Source Link

Document

This method begins an asynchronous query.

Usage

From source file:com.android.music.ArtistAlbumBrowserFragment.java

private Cursor getArtistCursor(AsyncQueryHandler async, String filter) {

    String[] cols = new String[] { MediaStore.Audio.Artists._ID, MediaStore.Audio.Artists.ARTIST,
            MediaStore.Audio.Artists.NUMBER_OF_ALBUMS, MediaStore.Audio.Artists.NUMBER_OF_TRACKS };

    Uri uri = MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI;
    if (!TextUtils.isEmpty(filter)) {
        uri = uri.buildUpon().appendQueryParameter("filter", Uri.encode(filter)).build();
    }//from   w w w .  j  av  a  2  s .c o m

    Cursor ret = null;
    if (async != null) {
        async.startQuery(0, null, uri, cols, null, null, MediaStore.Audio.Artists.ARTIST_KEY);
    } else {
        ret = MusicUtils.query(getActivity(), uri, cols, null, null, MediaStore.Audio.Artists.ARTIST_KEY);
    }
    return ret;
}

From source file:com.android.music.AlbumBrowserFragment.java

private Cursor getAlbumCursor(AsyncQueryHandler async, String filter) {
    String[] cols = new String[] { MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ARTIST,
            MediaStore.Audio.Albums.ALBUM, MediaStore.Audio.Albums.ALBUM_ART };

    Cursor ret = null;/*  w w  w. ja v  a 2  s.  c o  m*/
    if (mArtistId != null) {
        Uri uri = MediaStore.Audio.Artists.Albums.getContentUri("external", Long.valueOf(mArtistId));
        if (!TextUtils.isEmpty(filter)) {
            uri = uri.buildUpon().appendQueryParameter("filter", Uri.encode(filter)).build();
        }
        if (async != null) {
            async.startQuery(0, null, uri, cols, null, null, MediaStore.Audio.Albums.DEFAULT_SORT_ORDER);
        } else {
            ret = MusicUtils.query(getActivity(), uri, cols, null, null,
                    MediaStore.Audio.Albums.DEFAULT_SORT_ORDER);
        }
    } else {
        Uri uri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
        if (!TextUtils.isEmpty(filter)) {
            uri = uri.buildUpon().appendQueryParameter("filter", Uri.encode(filter)).build();
        }
        if (async != null) {
            async.startQuery(0, null, uri, cols, null, null, MediaStore.Audio.Albums.DEFAULT_SORT_ORDER);
        } else {
            ret = MusicUtils.query(getActivity(), uri, cols, null, null,
                    MediaStore.Audio.Albums.DEFAULT_SORT_ORDER);
        }
    }
    return ret;
}

From source file:com.android.music.AlbumBrowserActivity.java

private Cursor getAlbumCursor(AsyncQueryHandler async, String filter) {
    String[] cols = new String[] { MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ARTIST,
            MediaStore.Audio.Albums.ALBUM, MediaStore.Audio.Albums.ALBUM_ART };

    Cursor ret = null;//from   ww  w  . j  a  v a2  s.c o m
    if (mArtistId != null) {
        Uri uri = MediaStore.Audio.Artists.Albums.getContentUri("external", Long.valueOf(mArtistId));
        if (!TextUtils.isEmpty(filter)) {
            uri = uri.buildUpon().appendQueryParameter("filter", Uri.encode(filter)).build();
        }
        if (async != null) {
            async.startQuery(0, null, uri, cols, null, null, MediaStore.Audio.Albums.DEFAULT_SORT_ORDER);
        } else {
            ret = MusicUtils.query(this, uri, cols, null, null, MediaStore.Audio.Albums.DEFAULT_SORT_ORDER);
        }
    } else {
        Uri uri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
        if (!TextUtils.isEmpty(filter)) {
            uri = uri.buildUpon().appendQueryParameter("filter", Uri.encode(filter)).build();
        }
        if (async != null) {
            async.startQuery(0, null, uri, cols, null, null, MediaStore.Audio.Albums.DEFAULT_SORT_ORDER);
        } else {
            ret = MusicUtils.query(this, uri, cols, null, null, MediaStore.Audio.Albums.DEFAULT_SORT_ORDER);
        }
    }
    return ret;
}

From source file:com.android.music.PlaylistBrowserFragment.java

private Cursor getPlaylistCursor(AsyncQueryHandler async, String filterstring) {

    StringBuilder where = new StringBuilder();
    where.append(MediaStore.Audio.Playlists.NAME + " != ''");

    // Add in the filtering constraints
    String[] keywords = null;//  www  . j  ava  2 s  .com
    if (filterstring != null) {
        String[] searchWords = filterstring.split(" ");
        keywords = new String[searchWords.length];
        Collator col = Collator.getInstance();
        col.setStrength(Collator.PRIMARY);
        for (int i = 0; i < searchWords.length; i++) {
            keywords[i] = '%' + searchWords[i] + '%';
        }
        for (int i = 0; i < searchWords.length; i++) {
            where.append(" AND ");
            where.append(MediaStore.Audio.Playlists.NAME + " LIKE ?");
        }
    }

    String whereclause = where.toString();

    if (async != null) {
        async.startQuery(0, null, MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, mCols, whereclause, keywords,
                MediaStore.Audio.Playlists.NAME);
        return null;
    }
    Cursor c = null;
    c = MusicUtils.query(getActivity(), MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, mCols, whereclause,
            keywords, MediaStore.Audio.Playlists.NAME);

    return mergedCursor(c);
}

From source file:de.sindzinski.lpictrainer.TestActivity.java

public void onCheckAll() {
    Integer points = 0;/* www  .  j ava2s .co m*/
    Integer maxPoints = 0;

    //Uri mDataUrl = Uri.parse(TrainerContract.AnswerEntry.CONTENT_URI + "/"
    //        + mAnswer.index);
    String[] projection = { TrainerContract.AnswerEntry._ID, TrainerContract.AnswerEntry.COLUMN_ID,
            //                AnswerEntry.COLUMN_CHECKED,
            TrainerContract.AnswerEntry.COLUMN_POINTS, };
    //String selectionClause = TrainerContract.AnswerEntry.CO + "=" + mAnswer.index;
    String selectionClause = null;
    String[] selectionArgs = null;
    String sortOrder = null;

    //        try {
    //            Cursor cursor = this.getContentResolver().query(
    //                    AnswerEntry.CONTENT_URI,
    //                    projection,
    //                    selectionClause,
    //                    selectionArgs,
    //                    sortOrder);
    //
    //            if (cursor != null) {
    //                if (cursor.moveToFirst()) {
    //                    do {
    //                        //points = points + 1;
    //                        int rowPoints = Integer.parseInt(cursor.getString(cursor.getColumnIndexOrThrow(TrainerContract.QuestionEntry.COLUMN_POINTS)));
    //                        if (rowPoints > 0 ) {
    //                            points = points + 1;
    //                                    }
    //
    //                    } while (cursor.moveToNext());
    //                }
    //                // always close the cursor
    //                cursor.close();
    //            }
    //        } catch (SQLiteException e) {
    //            Logger.e(TAG, "Error reading database: " + e);
    //        }
    //        maxPoints = to - from;
    //        Toast.makeText(this,
    //                "You reached " + points.toString() + " out of " + maxPoints.toString(), Toast.LENGTH_LONG).show();

    int token = 2;
    AsyncQueryHandler handler = new AsyncQueryHandler(this.getContentResolver()) {
        @Override
        protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
            super.onQueryComplete(token, cookie, cursor);
            if (cursor != null) {
                Integer points = 0;
                Integer maxPoints = 0;
                if (cursor.moveToFirst()) {
                    do {
                        if (Integer.parseInt(cursor.getString(cursor
                                .getColumnIndexOrThrow(TrainerContract.QuestionEntry.COLUMN_POINTS))) > 0) {
                            points = points + 1;
                        }
                        //points = points + cursor.getColumnIndexOrThrow(TrainerContract.QuestionEntry.COLUMN_POINTS);
                    } while (cursor.moveToNext());
                }
                // always close the cursor
                cursor.close();
                maxPoints = to - from;
                Toast.makeText(de.sindzinski.lpictrainer.TestActivity.this,
                        "You reached " + points.toString() + " out of " + maxPoints.toString(),
                        Toast.LENGTH_LONG).show();
            }
        }
    };

    handler.startQuery(token, null, TrainerContract.AnswerEntry.CONTENT_URI, projection, selectionClause,
            selectionArgs, sortOrder);
}