Search code examples
androidarraylistandroid-sqliteandroid-log

Display ArrayList from SQLite database in logs


How I can display items from ArraylIst in Logs?

I have class DataBaseAdapter, where I have Method getCardsArrayList, now I want to display each item from this ArrayLIst in Logs

When I try to write this lines in my MainActivity:

ArrayList<Cards> cards= dataBaseAdapter.getCardsArrayList();
    for(int i=0; i<cards.size();i++){
        Log.i("WORKS",cards[i]);

    }

I have an error: Array type expected, found java.util.ArrayList<com.myproject.Cards>

Cards is my class with getters and setters

Arrayist in DataBaseAdapter:

public ArrayList<Cards> getCardsArrayList(){
    SQLiteDatabase sqLiteDatabase= helper.getWritableDatabase();

    Cursor cursor=sqLiteDatabase.rawQuery(helper.QUERY,null);

    cursor.moveToFirst();
    for (int i=0; i<cursor.getCount(); i ++){
        cardsArrayList.add(new Cards(cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getString(3)));

        cursor.moveToNext();
    }
    return cardsArrayList;
}

Solution

  • Use like this:

    ArrayList<Cards> cards= dataBaseAdapter.getCardsArrayList();
        for(int i=0; i<cards.size();i++){
            Log.i("WORKS",cards.get(i).toString());
    
        }
    

    To access the items of the arrayList you must use get method.

    And just to make sure the Cards class items must have a toString method implemented because the Log needs a String object as the second parameter.