我正在尝试从SQLite DB
获取数据,其中我成功地向其添加数据。我正在使用databaseHelper
类添加数据以及应用可能需要的任何其他查询。作为参考,这就是班级DataBaseHelper的样子
这是我的帮助器中的getData方法的样子:
public Cursor getBasicWorkoutInfo(){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT "+COL2 +", "+COL3+", "+COL4 +" FROM " + TABLE_NAME;
Cursor data = db.rawQuery(query, null);
return data;
}
"COL"
只是数据库中字段名称的字符串标记。
recyclerView
使用我创建的课堂训练arrayList
,这就是它的样子
public class Workout {
public String workoutName;
public String numberOfSets;
public String restBetweenSets;
}
在活动的onCreate
方法中,我设置了一个处理从数据库获取数据的方法,但在测试之后,应用程序崩溃并且调试控制台在第三行显示错误while loop。
Cursor data=mDatabaseHelper.getBasicWorkoutInfo();
Workout work=new Workout();
while(data.moveToNext()){
work.workoutName=data.getString(1);
work.numberOfSets= String.valueOf(data.getInt(2));
work.restBetweenSets=data.getString(3);
}
sampleWorkout.add(work);
if (sampleWorkout.isEmpty()){
createTextView.setVisibility(View.VISIBLE);
}else {
workoutRecyclerView=(RecyclerView) findViewById(R.id.myWorkoutsRecyclerView);
linearLayoutManager = new LinearLayoutManager(this);
DividerItemDecoration itemDecoration = new DividerItemDecoration(this, linearLayoutManager.getOrientation());
workoutRecyclerView.setLayoutManager(linearLayoutManager);
workoutRecyclerView.addItemDecoration(itemDecoration);
workoutRecyclerView.setHasFixedSize(true);
mWorkAdapter = new WorkoutsRecyclerAdapter(sampleWorkout, getBaseContext());
workoutRecyclerView.setAdapter(mWorkAdapter);
}
循环中可能出现什么问题,如何解决这个问题,以便能够有效地将数据添加到recyclerView
答案 0 :(得分:0)
试试这个
由于列索引在查询
中以0开头更改为:
work.workoutName=data.getString(0);
work.numberOfSets= String.valueOf(data.getInt(1));
work.restBetweenSets=data.getString(2);
相反
work.workoutName=data.getString(1);
work.numberOfSets= String.valueOf(data.getInt(2));
work.restBetweenSets=data.getString(3);
这是解决方案datatypes
使用您的表数据类型