我已将值存储在数据库中,现在我正在尝试从数据库中获取值。所有值都存储在数据库中,但是当我在String列表中获取值时,它只返回最后的值。以下是我的代码。任何帮助将不胜感激。请
public List GetBigRun(){
List<Run_Data_Model> bigRun_list = new ArrayList<>();
this.open();
Cursor cursor = database.query(MySQLiteHelper.TABLE_NAME_BIG_RUN,
allColumns_table_big_run, null,
null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Run_Data_Model mBigRunObj = new Run_Data_Model();
mBigRunObj.RunBtnState = cursor.getString(1);
mBigRunObj.RunImages = cursor.getString(2);
mBigRunObj.RunColor = cursor.getString(3);
mBigRunObj.RunDays = cursor.getString(4);
mBigRunObj.RunTime = cursor.getString(5);
bigRun_list.add(mBigRunObj);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
this.close();
return bigRun_list;
}
这是我创建的数据库。
public static final String TABLE_NAME_BIG_RUN ="big_run";
public static final String BIG_RUN_COLUMN_ID = "_id";
public static String BIG_RUN_BTN_STATE ="btn_state";
public static String BIG_RUN_BTN_IMAGE ="btn_image";
public static String BIG_RUN_BTN_COLOR ="btn_color";
public static String BIG_RUN_DAYS ="days";
public static String BIG_RUN_TIME ="time";
//Query for create table
private static final String DATABASE_CREATE_TABLE_BIG_RUN = "create table "
+ TABLE_NAME_BIG_RUN + "("+ BIG_RUN_COLUMN_ID
+ " integer primary key autoincrement, " + BIG_RUN_BTN_STATE
+ " text , " + BIG_RUN_BTN_IMAGE
+ " text , " + BIG_RUN_BTN_COLOR
+ " text , " + BIG_RUN_DAYS
+ " text , " + BIG_RUN_TIME
+ " text );";
这里我在数据库中保存值。
private void PutValues() {
mydata.DeleteAllBigRun();
/*if(mPref.GetRunCount().equals("0")) {*/
for (int i = 0; i < 73; i++) {
Run_Data_Model runModel = new Run_Data_Model();
runModel.RunDays = "Day " + (i + 12);
runModel.RunImages = String.valueOf(R.drawable.cross_icon);
runModel.RunBtnState = "0";
runModel.RunColor = "#e76025";
/* if (id == 1) {*/
sec = sec + 20;
int mins = sec / 60;
int remainder = sec - mins * 60;
int secs = remainder;
runModel.RunTime = mins + " min " + secs + " sec";
/* } else if (id == 2) {
sec = sec + 40;
int mins = sec / 60;
int remainder = sec - mins * 60;
int secs = remainder;
runModel.RunTime = mins + " min " + secs + " sec";
runModel.RunChallengeState = "2";
} else if (id == 3) {
runModel.RunTime = (i + 1) + " min ";
runModel.RunChallengeState = "3";
}*/
mydata.InsertBigRunInTable(runModel.RunBtnState, runModel.RunImages, runModel.RunColor, runModel.RunDays, runModel.RunTime);
}
mPref.SetRunCount("1");
//}
}
这就是我如何获取列表中的值并将其传递给适配器。
run_list = mydata.GetBigRun();
/* for(int i=0;i<run_list.size();i++) {
Log.d("run_list_size", run_list.get(i).RunDays);
}*/
gv = (GridView) findViewById(R.id.gridView1);
gv.setNumColumns(4);
RunChallengeAdapter adapter =new RunChallengeAdapter(this,run_list);
gv.setAdapter(adapter);
adapter.notifyDataSetChanged();
答案 0 :(得分:1)
if (cursor.moveToFirst()) {
do {
Run_Data_Model mBigRunObj = new Run_Data_Model();
mBigRunObj.RunBtnState = cursor.getString(1);
mBigRunObj.RunImages = cursor.getString(2);
mBigRunObj.RunColor = cursor.getString(3);
mBigRunObj.RunDays = cursor.getString(4);
mBigRunObj.RunTime = cursor.getString(5);
bigRun_list.add(mBigRunObj);
}while (cursor.moveToNext());
}
使用上面的代码并检查
答案 1 :(得分:0)
我自己找到了解决方案。 我的代码中的每一件事都是正确和有效的,我唯一的错误就是我的模型对象。我无意中添加了静态的对象,这会产生问题。