这是一种基于特定列值随机混洗某些数据库表条目的简单方法。感谢 cbrulak 的帮助,它现在可以正常运行。它无论如何都不是最有效的,但我想我会在修复后发布它,因为有人可能觉得它有点用处。
/* The method will shuffle the specific entries in the table, based on column value */
public void shuffleDeck(int deck_id){
int i, j, count;
int loop1, loop2;
int num;
int id1, id2;
Random rand = new Random();
String front1, back1, front2, back2;
/* Create two separate content values for swapping the values between the two rows */
ContentValues updated_1 = new ContentValues();
ContentValues updated_2 = new ContentValues();
String[] columns = new String[]{CARD_ROWID, CARD_FRONT, CARD_BACK};
/* Create two cursors pointing to the same query */
Cursor cursor1 = Database.query(CARDS_TABLE, columns, DECK_ROWID + " = " + deck_id,
null, null, null, null);
Cursor cursor2 = Database.query(CARDS_TABLE, columns, DECK_ROWID + " = " + deck_id,
null, null, null, null);
cursor1.moveToFirst();
cursor2.moveToFirst();
int iRow = cursor1.getColumnIndex(CARD_ROWID);
int iFront = cursor1.getColumnIndex(CARD_FRONT);
int iBack = cursor1.getColumnIndex(CARD_BACK);
/* Get total number of entries in the cursor */
count = cursor1.getCount();
/* Loop through */
for(i=1; i<=count; i++){
num = rand.nextInt(count) + 1;
/* Acquire the values from the current row that will be inserted into the randomly selected row */
id1 = cursor1.getInt(iRow);
front1 = cursor1.getString(iFront);
back1 = cursor1.getString(iBack);
/* Move to the next row in cursor2 for a random number of times */
for (j=1; j<num; j++){
cursor2.moveToNext();
/* Fail safe to make sure it doesn't go out of bounds */
if(cursor2.isLast())
break;
}
/* Acquire the values from the random row */
front2 = cursor2.getString(iFront);
back2 = cursor2.getString(iBack);
id2 = cursor2.getInt(iRow);
/* Check to see if the second cursor is also pointing to the same position. */
if(!(front1.equals(front2) && back1.equals(back2))){
updated_1.put(CARD_FRONT, front2);
updated_1.put(CARD_BACK, back2);
updated_2.put(CARD_FRONT, front1);
updated_2.put(CARD_BACK, back1);
/* Call the database to UPDATE the values */
Database.update(CARDS_TABLE, updated_1, CARD_ROWID + " = " + id1, null);
Database.update(CARDS_TABLE, updated_2, CARD_ROWID + " = " + id2, null);
/* Clear the content values */
updated_1.remove(CARD_FRONT);
updated_1.remove(CARD_BACK);
updated_2.remove(CARD_FRONT);
updated_2.remove(CARD_BACK);
/* Requery the cursors */
cursor1 = Database.query(CARDS_TABLE, columns, DECK_ROWID + " = " + deck_id,
null, null, null, null);
cursor2 = Database.query(CARDS_TABLE, columns, DECK_ROWID + " = " + deck_id,
null, null, null, null);
/* Move cursor1 to the same position it was, right before requerying it */
for(loop1 = 1; loop1<=i; loop1++){
cursor1.moveToNext();
/* Fail save in case it tries to go out of bound */
if(cursor1.isLast())
break;
}
/* Move cursor2 to the same position it was, right before requerying it */
for(loop2 = 1; loop2<=j; loop2++){
cursor2.moveToNext();
/* Fail save in case it tries to go out of bound */
if(cursor2.isLast())
break;
}
}
/* Move the cursors to the new positions */
cursor1.moveToNext();
cursor2.moveToFirst();
}
cursor1.close();
cursor2.close();
}
答案 0 :(得分:1)
在更新后请求新光标。
请参阅:Android - Can you update a Cursor for SQLite results?
更重要的是:http://developer.android.com/reference/android/database/Cursor.html#requery()(他们说要求新的)
奖励积分:使用内容提供商。为您节省大量头痛。