简体   繁体   中英

Updating an ArrayList with items from sqlite database

I'm trying to fetch data from sqlite database and update a custom recycler view with these data. I've created a method in my Database helper class that returns an ArrayList of items fetched from the database. This is how I expect the method to work:

  1. query the database, store the result set in a Cursor object.
  2. Loop through the result set in the Cursor object and add each item to the end of an ArrayList object.
  3. Return the ArrayList object containing the items fetched from the database.

But this is how the method is working:

  1. queries the database, stores the result set in a Cursor object. Correct.
  2. On the first iteration of the result set, the first item in the result set is added at index 0 of the ArrayList object. Just as expected. But on the second iteration, the second item in the result set replaces the already added item in the ArrayList at index 0, and it's also added at index 1. Furthermore, on the third iteration, the third item in the result set replaces all the items in the ArrayList such that we now have only the third item at indices 0, 1 and 2 instead of having the first item at index 0, second at index 1 and third at index 2. I'd really appreciate all the help I can get on this.

Below is the snippet of the method that does the item fetching from the database

public ArrayList<RechargeCard> getCard(){
    ArrayList<RechargeCard>  list = new ArrayList<>();
    RechargeCard card = new RechargeCard();
    SQLiteDatabase database = getReadableDatabase();
    String query = "SELECT * FROM " + RechargeEntry.TABLE_NAME;
    Cursor cursor = database.rawQuery(query, null);

    if (cursor.moveToFirst()){
        do{
            String name = cursor.getString(cursor.getColumnIndexOrThrow(RechargeEntry.NAME_COLUMN));
            int price = cursor.getInt(cursor.getColumnIndexOrThrow(RechargeEntry.PRICE_COLUMN));
            double quantity = cursor.getDouble(cursor.getColumnIndexOrThrow(RechargeEntry.QUANTITY_COLUMN));
            double total = cursor.getDouble(cursor.getColumnIndexOrThrow(RechargeEntry.TOTAL));

            card.setName(name);
            card.setPrice(price);
            card.setQuantity(quantity);
            card.setTotal(total);
            list.add(card);
        } while (cursor.moveToNext());
    }
    cursor.close();
    database.close();
    return list;
}

What you code is doing is that it is only updating RechargeCard card = new RechargeCard(); for every iteration that the while loop is doing. You should put RechargeCard card = new RechargeCard(); inside the do while loop so it will create new instances every iteration

public ArrayList<RechargeCard> getCard(){
    ArrayList<RechargeCard>  list = new ArrayList<>();
   
    SQLiteDatabase database = getReadableDatabase();
    String query = "SELECT * FROM " + RechargeEntry.TABLE_NAME;
    Cursor cursor = database.rawQuery(query, null);

    if (cursor.moveToFirst()){
        do{
         RechargeCard card = new RechargeCard();
            String name = cursor.getString(cursor.getColumnIndexOrThrow(RechargeEntry.NAME_COLUMN));
            int price = cursor.getInt(cursor.getColumnIndexOrThrow(RechargeEntry.PRICE_COLUMN));
            double quantity = cursor.getDouble(cursor.getColumnIndexOrThrow(RechargeEntry.QUANTITY_COLUMN));
            double total = cursor.getDouble(cursor.getColumnIndexOrThrow(RechargeEntry.TOTAL));

            card.setName(name);
            card.setPrice(price);
            card.setQuantity(quantity);
            card.setTotal(total);
            list.add(card);
        }while (cursor.moveToNext());
    }
    cursor.close();
    database.close();
    return list;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM