我是android开发的新手。我想要的是从存储在数据库中的值填充我的列表。为此我创建了一个带有insert和select语句的数据库。同时我创建了一个模型来保存data。从列表适配器我已经将数据插入到数据库中,但是当我尝试检索该数据时,我得到了空。
DataBase Class
public class DbHandler extends SQLiteOpenHelper {
private static final int dbVersion = 1;
private static final String dbName = "HSsuraksha";
private static final String tableName = "pocketDocs";
private static final String userId = "userId";
private static final String docId = "docId";
private static final String fileName = "fileName";
private static final String fileExt = "fileExt";
private static final String title = "title";
private static final String createTable = "CREATE TABLE " + tableName + "(" + userId + " Integer Primary Key," + docId + " Integer," + fileName + " Text," + fileExt + " Text," + title + " Text" + ")";
public DbHandler(Context context) {
super(context, dbName, null, dbVersion);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {
}
public void insertData(DbModel dbModel) {
SQLiteDatabase database = getWritableDatabase();
ContentValues contentValues = new ContentValues();
database.beginTransaction();
contentValues.put(userId, dbModel.userId);
contentValues.put(docId, dbModel.docId);
contentValues.put(fileName, dbModel.fileName);
contentValues.put(fileExt, dbModel.fileExtension);
contentValues.put(title, dbModel.title);
if (contentValues != null) {
Long id = database.insert(tableName, null, contentValues);
Log.e("insert values", "" + id);
}
database.endTransaction();
database.close();
}
public DbModel selectDocs(String id) {
SQLiteDatabase db = this.getReadableDatabase();
DbModel model = new DbModel();
Cursor cursor = db.query(tableName, new String[]{fileName, fileExt, title}, docId + "=?", new String[]{id}, null, null, null, null);
if (cursor.moveToFirst()) {
model.fileName = cursor.getString(2);
model.fileExtension = cursor.getString(3);
model.title = cursor.getString(4);
}
db.close();
return model;
}
}
数据库模型
public class DbModel implements Serializable {
public String userId, docId, fileName, fileExtension, title;
public DbModel() {
}
public DbModel(String userId, String docId, String fileName, String fileExtension, String title) {
this.userId = userId;
this.docId = docId;
this.fileName = fileName;
this.fileExtension = fileExtension;
this.title = title;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getDocId() {
return docId;
}
public void setDocId(String docId) {
this.docId = docId;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFileExtension() {
return fileExtension;
}
public void setFileExtension(String fileExtension) {
this.fileExtension = fileExtension;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
ListAdapter
public View getView(int i, View view, ViewGroup viewGroup) {
final ViewHolder holder;
dbHandler = new DbHandler(context);
dbModel = new DbModel("1", "1", "GoogleDocs", ".pdf", "Test");
dbHandler.insertData(dbModel);
if (view == null) {
holder = new ViewHolder();
view = LayoutInflater.from(context).inflate(R.layout.view_docs_custom_list, viewGroup, false);
holder.docsImage = (ImageView) view.findViewById(R.id.iv_docs_image);
holder.tvTitle = (TextView) view.findViewById(R.id.tv_file_name);
holder.tvPostedOn = (TextView) view.findViewById(R.id.tv_posted_on);
holder.btShare = (Button) view.findViewById(R.id.bt_share);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
dbHandler.selectDocs(dbModel.getDocId()); // here i am getting the values from db and displaying it in the list
holder.tvTitle.setText(dbModel.title);
答案 0 :(得分:0)
更改
model.fileName = cursor.getString(2);
model.fileExtension = cursor.getString(3);
model.title = cursor.getString(4);
到
model.fileName = cursor.getString(cursor.getColumnIndexOrThrow(fileName));
model.fileExtension = cursor.getString(cursor.getColumnIndexOrThrow(fileExt));
model.title = cursor.getString(cursor.getColumnIndexOrThrow(title));
getString
基于零索引。它取决于您在query
中提到的列数,而不是您在CREATE
声明中使用它们的顺序
如果您尝试
cursor.getString(3);
你应该得到IllegalStateException
陈述
java.lang.IllegalStateException: Couldn't read row 0, col 3 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
因为您尝试访问cursor
中的第4列,而它只有3列