Search code examples
androidjsonsqliteandroid-tvleanback

Replacing json retrieval with database retrieval in project


Apologies if I'm not providing enough information here, I'm new to android/java and am learning by combining elements of various example projects.

I have a working VerticalGridFragment page for an Android TV project (using leanback) that loads album cover art and details in columns and rows. It borrows heavily from the googlesamples example on github.

Using a test music_database_proto.json file in the format of:

{
  "cards": [
    {
      "type": "GRID_SQUARE",
      "title": "Around the World in a Day",
      "artist": "Prince and the Revolution",
      "description": "Prince and the Revolution",
      "groupType": 0,
      "tagID": 0,
      "localImageResource": "food_01"
    }
}

I can then load this using a createRows() function (from the same sample project) using:

PresenterSelector cardPresenterSelector = new CardPresenterSelector(getActivity());
mAdapter = new ArrayObjectAdapter(cardPresenterSelector);
String json = Utils.inputStreamToString(getResources().
    openRawResource(R.raw.music_database_proto));
CardRow row = new Gson().fromJson(json, CardRow.class);
mAdapter.addAll(0, row.getCards());

Which uses the sample projects classes of CardRow.class, Card.class and their various supporting classes in the example.

I've implemented an SQLite database that successfully initiates and I can call various results in the Logs of Android Studio. I don't know where to start with inserting results from the database as Cards that I can load into the VerticalGridFragment, replacing the function of the json file?

I thought it would be as simple as creating a List<Card> from database results and adding this to mAdapter using mAdapter.addAll. However, I've had little luck in implementing a List of Card objects using iterating code. Desperately, I thought I should turn database results into a json string and feed them into the existing code I showed above?

Any help appreciated. The database is as basic as a recreation of the json variables and can be adjusted to suit.


Solution

  • What exactly do you mean by However, I've had little luck in implementing a List of Card objects using iterating code.? If you're calling mAdapter.addAll(0, cards) with a list of cards, it should work as you desire.

    Are you having difficulty extracting the list of cards from your SQLite database? You can rely on this link which has the following answer. I've modified it slightly to be about Card.class. You'll also need to add a constructor to the Card object since they provided it to you assuming you'd use @SerializedName to initialize it. The below example assumes you've added a constructor that looks like public Card(String title, String description).

    List<Card> cardsList = new ArrayList<Card>();
    database = openOrCreateDatabase("your_db", SQLiteDatabase.OPEN_READWRITE, null);
    if(database!=null)
    {
        c= database.rawQuery(QUERY, null);
        c.moveToFirst();
        while(c.isAfterLast()==false)
        {
            Card card = new Card(c.getString(c.getColumnIndex("title")),
                                 c.getString(c.getColumnIndex("description")));
            cardsList.add(card);
            c.moveToNext();
        }
    
    }
    database.close();
    c.close();
    

    The example is really just a starting point, but they encourage you to customize any of those classes within the sample to better fit your needs.

    I also like that you're trying to learn SQLite since it's a piece of Android (and mobile development in general) that is good to be familiar with. You can also use a library like Stetho by Facebook to make sure your SQLite tables actually contain the correct data. It makes debugging a little easier.