Shuffle the id of table to get details in android

馋奶兔 提交于 2019-12-25 02:08:08

问题


I have student table , which has four fiels id, name, address and age. In this table 50 student. My question is , How to shuffle the id and get that id's details( id, name, address and age) and get the one id at a time and if i click the next button then next student details.

DatabaseHeper.java

 // I donot understand which code write in DatabaseHeper.java file 
 /*
public Cursor shuffleJokes(int [] id) {

        Cursor c = null;
    c = myDataBase.rawQuery("select _id,title,body from jokes where title ="
            + id, null);
    return c;
}*/

public void shuffleJokes(int [] ar) {       

Random rnd = new Random();
for (int i = ar.length - 1; i >= 0; i--)
{
  int index = rnd.nextInt(i + 1);
  // Simple swap
  int a = ar[index];
  ar[index] = ar[i];
  ar[i] = a;
}

shuffleStudnt.java

DatabaseHelper db;
Cursor cur
cur=db.shuffleArray(arrayofid);  // how to get arrayofid array which is id of student table
cur.moveToFirst();    

I have problem, in how to get table id in array. Please give me best solution which code write in DatabaseHeper.java and shuffleStudent.java file.


回答1:


I think you want to select a random student from the table every time you tap a button, right?

Then I would query all students, save them to an array and then select one randomly from that array everytime the user taps the button.



来源:https://stackoverflow.com/questions/7831857/shuffle-the-id-of-table-to-get-details-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!