Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9133855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:33:30+00:00 2026-06-17T08:33:30+00:00

This is a simple method to randomly shuffle certain database table entries based on

  • 0

This is a simple method to randomly shuffle certain database table entries based on a specific column value. Thanks to cbrulak‘s help, it now functions appropriately. It is not the most efficient by any means, but I thought I would just post it after the fix, as someone may find it somewhat useful.

/* 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();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:33:30+00:00Added an answer on June 17, 2026 at 8:33 am

    request a new cursor after the update(s).

    See: Android – Can you update a Cursor for SQLite results?

    And more important: http://developer.android.com/reference/android/database/Cursor.html#requery() (they say to request new ones)

    Bonus points for you: use a content provider. Save you tons of headache.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have this simple method: public IEnumerable<uint> GetNumbers() { uint n = 0;
Hi I have this simple method which gets triggered when i click inside a
This simple Python method I put together just checks to see if Tomcat is
I've got a simple method that does this: private void searchButton_Click(object sender, EventArgs e)
So I have a simple method called Invert : public static void Invert(this bool
hopefully a pretty simple question this time. I have a Select method in a
in iPhone, this would be simple---each view has a scrollViewDidScroll method. I am trying
Which method is very effective and simple to code to attain this functionality ?
I think I've settled on this as the most simple and unit-testable method for
I've written this simple method to give me the date of every pay period

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.