Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用游标和交换值洗牌数据库表项_Android_Database_Sqlite_Shuffle - Fatal编程技术网

Android 如何使用游标和交换值洗牌数据库表项

Android 如何使用游标和交换值洗牌数据库表项,android,database,sqlite,shuffle,Android,Database,Sqlite,Shuffle,这是一种基于特定列值随机洗牌某些数据库表项的简单方法。多亏了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 lo

这是一种基于特定列值随机洗牌某些数据库表项的简单方法。多亏了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();
}
/*该方法将根据列值洗牌表中的特定条目*/
公共空洗舱(内部甲板id){
int i,j,计数;
int loop1,loop2;
int-num;
INTID1,id2;
Random rand=新的Random();
字符串front1,back1,front2,back2;
/*创建两个单独的内容值,以便在两行之间交换值*/
更新的ContentValues_1=新的ContentValues();
ContentValues updated_2=新ContentValues();
String[]columns=新字符串[]{CARD\u ROWID,CARD\u FRONT,CARD\u BACK};
/*创建指向同一查询的两个游标*/
Cursor cursor1=数据库.query(卡片表、列、卡片行id+“=”+卡片行id、,
空,空,空,空);
Cursor cursor2=数据库.query(卡片表、列、卡片行id+“=”+卡片行id、,
空,空,空,空);
游标1.moveToFirst();
游标2.moveToFirst();
int iRow=cursor1.getColumnIndex(CARD_ROWID);
int-iffront=cursor1.getColumnIndex(卡片前);
int iBack=cursor1.getColumnIndex(卡片返回);
/*获取光标中的项目总数*/
count=cursor1.getCount();
/*循环通过*/

对于(i=1;i在更新后请求新光标)

见:

更重要的是:(他们说要申请新的)


给你的额外好处是:使用内容提供商。为你省去了很多麻烦。

问题是,我可以看到光标在不同的行中正确移动,而使用不同的调用不会出现问题。问题是更新查询没有像应该的那样更新值。就像他们只是拒绝更新值一样我相信问题是……我会检查一下,然后再联系你。谢谢你的帮助!重新询问解决了所有问题。我非常感谢你的帮助!