首先让我解释一下如何从sqlite插入和返回数据。
首先,我创建这样的表:
private static final String CREATE_TABLE_STUDENT_LESSON = " create table STUDENTSPECIFICLESSON ( _id TEXT , _viewID INTEGER PRIMARY KEY AUTOINCREMENT , _LESSON_TITLE TEXT , _LESSON_DATE TEXT , _LESSON_PROBLEM TEXT );";
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_STUDENT_LESSON);
}
然后在sqlite中这样插入到表中:
public void insertLesson(String _id, String lessonTitle, String lessonDate, String lessonProblem) {
ContentValues contentValue = new ContentValues();
contentValue.put(SQLiteHelper._ID, _id);
contentValue.put(SQLiteHelper.LESSON_TITLE, lessonTitle);
contentValue.put(SQLiteHelper.LESSON_DATE, lessonDate);
contentValue.put(SQLiteHelper.LESSON_PROBLEM, lessonProblem);
this.getWritableDatabase().insert(SQLiteHelper.TABLE_NAME_STUDENTSPECIFICLESSON, null, contentValue);
}
我从这样的活动中插入:
sqLiteHelper.insertLesson(id,etLessonTitle.getText().toString(),currentDateandTime,etLessonProbStu.getText().toString());
您会注意到我已经创建了
_viewID INTEGER PRIMARY KEY AUTOINCREMENT
我正在使用它来获取适配器中的特定viewholder。我通过这样做得到这个:
public String getLessonViewHolderID(String _path){
String id = null;
Cursor cursor = getReadableDatabase().rawQuery("Select "+SQLiteHelper._viewID+" from "+SQLiteHelper.TABLE_NAME_STUDENTSPECIFICLESSON+" Where "
+SQLiteHelper.LESSON_TITLE +"='"+_path+"'",null);
if (cursor != null) {
cursor.moveToFirst();
}
if (cursor == null) {
} else if (cursor.moveToFirst()) {
do {
id = String.valueOf(cursor.getInt(cursor.getColumnIndex(SQLiteHelper._viewID)));
} while (cursor.moveToNext());
cursor.close();
} else {
}
return id;
}
这很好地工作,并按预期正确返回
_viewID
。然后,我尝试通过执行以下操作从sqlite获取
_viewId
:public String getLessonProblem(String _id){
String id = null;
Cursor cursor = getReadableDatabase().rawQuery("Select _LESSON_PROBLEM from "+SQLiteHelper.TABLE_NAME_STUDENTSPECIFICLESSON+" Where "
+SQLiteHelper._viewID +"='"+_id+"'",null);
if (cursor != null) {
cursor.moveToFirst();
}
if (cursor == null) {
} else if (cursor.moveToFirst()) {
do {
id = String.valueOf(cursor.getInt(cursor.getColumnIndex("_LESSON_PROBLEM")));
} while (cursor.moveToNext());
cursor.close();
} else {
}
return id;
}
从我的
_LESSON_PROBLEM
中,我打电话给:String lessonProblem = helper.getLessonProblem(viewId);
上述
Adapter
将返回String lessonProblem
。我已经检查了我的数据库,所有数据都正确插入,下面是我的数据库的图像:
我不明白为什么它会返回0,但显然不是,有人能帮我指出问题所在吗?
最佳答案
id = String.valueOf(cursor.getInt(cursor.getColumnIndex("_LESSON_PROBLEM")));
得到一个字符串值作为int,然后将其转换为字符串。把它改成
id = cursor.getString(cursor.getColumnIndex("_LESSON_PROBLEM"));