【发布时间】:2016-02-17 04:37:04
【问题描述】:
我正在通过网络以 JSON 格式下载一些数据,在检索到它之后,我将它存储在 SqlLiteDatabase 中。之前我用来获取数据并将其显示在 RecyclerView 中,但现在我将其存储在 SqlLite 数据库中,然后将其显示在 RecyclerView 中。但是,数据在返回 true 时成功存储,但是当我显示数据时,它什么也没显示。要检查添加后数据是否存在,我会获取可用数据的计数并返回 0。
代码如下:
public boolean create(List<PGCardItemReturn> objectPg) {
SQLiteDatabase db = this.getWritableDatabase();
PGCardItemReturn lastItem = null;;
db.beginTransaction();
String sql = "Insert or Replace into tenant_pg (id,furnishing,area,price,numberofbeds,image_url) values(?,?,?,?,?,?)";
SQLiteStatement insert = db.compileStatement(sql);
for(int i=0;i<objectPg.size();i++){
PGCardItemReturn item = objectPg.get(i);
if(i == (this.count()-1)){
lastItem = item;
}
insert.bindString(1, item.getId());
insert.bindString(2, item.getFurnishing());
insert.bindString(3, item.getArea());
insert.bindString(4, item.getPrice());
insert.bindString(5, item.getNumberofbeds());
insert.bindString(6, item.getImageUrl());
insert.execute();
}
db.endTransaction();
return true;
}
在主要活动中:
boolean add = new TableControllerTenant_pg(getApplicationContext()).create(listPG);
【问题讨论】:
-
所以你说你的插入工作但你的显示没有,然后你发布了工作的代码 - 我很困惑。
-
检查您的数据库文件以确保数据已插入数据库中