Populate recyclerview with a SQLite database?

后端 未结 1 816
暗喜
暗喜 2021-01-07 07:41

There\'s nothing wrong with the typical android cursor. But I\'m still confused on how to implement it, does anyone here have an example or does anyone have another easier s

1条回答
  •  有刺的猬
    2021-01-07 08:44

    It was pretty easy to do, I just used a cursor to retrieve all the data in my database and stored them into a black object then an array list.

     public ArrayList getAllData() {
        ArrayList allGames = new ArrayList<>();
        SQLiteDatabase db = getWritableDatabase();
        String[] columns = {COLUMN_ID, COLUMN_NAME, COLUMN_PLATFORM, COLUMN_DATE};
        Cursor cursor = db.query(TABLE_GAMES, columns, null, null, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            do {
                //create a new Games object and retrieve the data from the cursor to be stored in this Games object
                Games game = new Games();
                //each step is a 2 part process, find the index of the column first, find the data of that column using
                //that index and finally set our blank Games object to contain our data
                game.set_id(cursor.getInt(cursor.getColumnIndex(COLUMN_ID)));
                game.set_name(cursor.getString(cursor.getColumnIndex(COLUMN_NAME)));
                game.set_platform(cursor.getString(cursor.getColumnIndex(COLUMN_PLATFORM)));
                game.set_releaseDate(cursor.getString(cursor.getColumnIndex(COLUMN_DATE)));
    
                allGames.add(game);
            } while (cursor.moveToNext());
        }
        return allGames;
    }
    

    0 讨论(0)
提交回复
热议问题