【问题标题】:Read from Custom List Object从自定义列表对象中读取
【发布时间】:2015-09-20 13:39:12
【问题描述】:

我在 Sqlite 数据库中有输入项和一个定义如下的方法:

// 获取所有的 ComplainMessageWhatevers 公共列表 getAllComplainMessageWhatever() {

List<ComplainMessageWhatever> cmwList = new ArrayList<ComplainMessageWhatever>();
// Select All Query
String selectQuery = "SELECT  * FROM " + TABLE_COMPLAIN_MESSAGE_WHATEVER;

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

// looping through all rows and adding to list
if (cursor.moveToFirst()) {
    do {
        ComplainMessageWhatever cmw = new ComplainMessageWhatever();
        cmw.setID(Integer.parseInt(cursor.getString(0)));
        cmw.setName(cursor.getString(1));
        cmw.setPhoneNumber(cursor.getString(2));
        // Adding cmw to list
        cmwList.add(cmw);
    } while (cursor.moveToNext());
}

// return cmw list
return cmwList;

}

在我的活动中,我这样做:

  DatabaseHandler dh = new DatabaseHandler(this);


        List<ComplainMessageWhatever> d1 = dh.getAllComplainMessageWhatever();

现在我不知道如何从该列表中提取值,例如电话号码、消息等,并将它们显示在 RecyclerView(Mateiral Design Cards) 中。

我该怎么做?我应该发布 DatabaseHandler 类吗?

【问题讨论】:

  • 在 CMW 类中编写 getter 方法并使用该方法访问数据

标签: android android-listview android-sqlite android-recyclerview


【解决方案1】:

由于您在自定义类中编写了 setter 方法,因此类似地编写 getter 方法

喜欢-

public int getId(){
 return id;
}

编写类似的方法来访问其他数据 和 在您的“RecyclerView”适配器“bindViewHolder”方法中,首先获取对您的自定义类对象的引用

喜欢-

YourCustomClass customClass = customclassList.get(position);

int id = customClass.getId()

并以类似方式访问其他数据

【讨论】:

    猜你喜欢
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-06
    • 1970-01-01
    • 1970-01-01
    • 2015-05-08
    相关资源
    最近更新 更多