【问题标题】:Display ArrayList from SQLite database in logs在日志中显示来自 SQLite 数据库的 ArrayList
【发布时间】:2015-04-11 21:51:25
【问题描述】:

如何在日志中显示来自 ArraylList 的项目?

我有类 DataBaseAdapter,我有方法 getCardsArrayList,现在我想在日志中显示这个 ArrayLIst 中的每个项目

当我尝试在我的 MainActivity 中编写这些行时:

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

    }

我有一个错误:Array type expected, found java.util.ArrayList&lt;com.myproject.Cards&gt;

卡片是我的带有getter和setter的类

DataBaseAdapter中的Arrayist:

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;
}

【问题讨论】:

    标签: android arraylist android-sqlite android-log


    【解决方案1】:

    这样使用:

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

    要访问 arrayList 的项目,您必须使用 get 方法。

    只是为了确保 Cards 类项必须实现一个 toString 方法,因为 Log 需要一个 String 对象作为第二个参数。

    【讨论】:

    • 不客气 :) 然后将其标记为答案。请:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 2012-10-16
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    • 2017-09-20
    相关资源
    最近更新 更多