1

There are 2 tables: High Score and Players with High Score table referencing PlayerID of player table. I am using cursorLoader to load data. I am not sure how to use cursor loader so that it returns Player name from players where player id is in High score.

I am not able to understand the answer from Join Two tables and get the cursor for Cursorloader, how to use join query in CursorLoader when its constructor does not support it. I need to use a new Uri, but I am not sure how to create it. Below are the table columns and the cursor loader query.

package colormemory.com.androidrecyclerviewgridview.model.DB;

import android.net.Uri;
import android.provider.BaseColumns;


public final class ColorGameContract {

// Authority identifier for the player content
public static final String AUTHORITY = "com.colormemory.PlayersProvider";

// URL to communicate with the player content provider
private static final Uri BASE_URI = Uri.parse("content://" + AUTHORITY);

public static final class Player
        implements BaseColumns {
    public static String PLAYER_TABLE_NAME = "players";


    public static final Uri PLAYER_CONTENT_URI =
            BASE_URI.buildUpon()
                    .appendPath(PLAYER_TABLE_NAME)
                    .build();
    public static final String COLUMN_SCORE = "score";
    public static final String COLUMN_NAME = "name";
}

public static final class HighScore
        implements BaseColumns {
    public static String HIGHSCORE_TABLE_NAME = "HighScore";


    public static final Uri HIGHSCORE_CONTENT_URI =
            BASE_URI.buildUpon()
                    .appendPath(HIGHSCORE_TABLE_NAME)
                    .build();
    public static final String COLUMN_SCORE = "score";
    public static final String COLUMN_PLAYERID = "playerId";
}

}

 @Override
 public Loader<Cursor> onCreateLoader(final int id, final Bundle args) {
    switch (id) {
        case LOADER_ID:
            String URL = PlayersProvider.HighScoreURL;

            Uri highScoreUri = Uri.parse(URL);
             String[] PROJECTION = {
                    //ColorGameContract.Player.COLUMN_NAME,
                    ColorGameContract.HighScore.COLUMN_SCORE};

            //final Uri uri = Uri.parse("content://some_uri");
            //final Uri uri = PLAYER_CONTENT_URI;
            return new CursorLoader(this, highScoreUri, PROJECTION, null, null, null);
    }
Community
  • 1
  • 1
user4057066
  • 295
  • 3
  • 14
  • 1
    use `String URL = PlayersProvider.HighScoreWithJoinURL;` ... and add implementation in `ContentProvider` which would handle this uri – Selvin Nov 30 '16 at 15:44
  • Got it! Works for me. Once I got the idea, it was easy. – user4057066 Dec 02 '16 at 06:04
  • 1
    @user4057066 could you please post an answer with your solution? I'm working with code somebody else wrote (and I don't have a lot of knowledge about CursorLoaders) and I have exactly the same problem and have zero idea how to write the join... – NoHarmDan Aug 17 '17 at 14:46

0 Answers0