我对此代码有疑问;
m_ProgressDialog = ProgressDialog.show(Main.this, "Please wait...",
"Writing Grid Data...", true);
new Thread() {
public void run() {
try {
Integer count = 1;
Integer data = 0;
SQLiteDatabase db = events.getWritableDatabase();
ContentValues values = new ContentValues();
Cursor cursor = db.query(TABLE_NAME, FROM, null, null,
null, null, null);
while (count <= pickedcount) {
startManagingCursor(cursor);
if (pick[count] != 0) {
if (pick[count] == 80) {
cursor.moveToLast();
data = (cursor.getInt(1) + 1);
values.put(num, data);
} else {
cursor.moveToPosition(pick[count]);
data = (cursor.getInt(1) + 1);
values.put(num, data);
Log.e("count ", "count = " + count.toString());
Log.e("Position ",
"Pos = " + cursor.getPosition());
Log.e("pick ", "pick = " + picked[count]
+ " + " + data.toString() + " cursor "
+ cursor.getString(1));
}
data = 0;
}
db.update(TABLE_NAME, values,
_ID + " = " + pick[count], null);
values.clear();
count++;
}
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
// dismiss the progressdialog
m_ProgressDialog.dismiss();
}
}.start();
Intent i = new Intent(Main.this, Main.class);
startActivity(i);
}
数据库是80行乘2列。第一列是_id,第二列是_id被记录的次数。 pick [array]保存需要记录的数字1-80,因此while循环应该移动到位置(1-80)拉出当前数字并简单地加1。
此方法在您第一次输入数据时效果很好,第二次此方法从数据库中为最后一个数字提取0。
以下是来自logcat的那些日志的输出。第一次选择数字1 - 5
count = 1
Pos = 1
pick = 1 + 1 cursor 0
count = 2
Pos = 2
pick = 2 + 1 cursor 0
count = 3
Pos = 3
pick = 3 + 1 cursor 0
count = 4
Pos = 4
pick = 4 + 1 cursor 0
count = 5
Pos = 5
pick = 5 + 1 cursor 0
并且第二次选择相同的数字:(上一次运行在数据库中成功记录1 - 5与1s)
count = 1
Pos = 1
pick = 1 + 2 cursor 1
count = 2
Pos = 2
pick = 2 + 2 cursor 1
count = 3
Pos = 3
pick = 3 + 2 cursor 1
count = 4
Pos = 4
pick = 4 + 2 cursor 1
count = 5
Pos = 5
pick = 5 + 1 cursor 0 //<----------
(注意所选数字5的输出)