you need to return random text by clicking from name in sqlite, put in textview. Fumbled in the internet, nothing comes out. Prompt how to register it, after the reference to a DB through the cursor. Or there may be another way. Thank.

case R.id.btn_text: Cursor c = db.query("mytable", null, null, null, null, null, null); } // закрываем подключение к БД dbHelper.close(); } 
  • one
    And order by random() limit 1 does not fit? (the first that Google gives out) - Mike

2 answers 2

  1. Beat in Google:

android cursor random

  1. Follow the first link

  2. Copy yourself the code:

     Cursor cursor = this.db.query("mytable Order BY RANDOM() LIMIT 1", new String[] { "*" }, null, null, null, null, null); 
  3. Run the code.

  4. Compare the expected results with those obtained.
  5. In case of a mismatch, repeat p1-5, incrementing the sequence number of the link from p.2
  • Thanks, the problem was in the launch through the phone, I do not know why, it went through the emulator. - Romik romikromik

That is, it is assumed that you have stored string names in the table, and you need to get one of them with the help of a random house? Then there will be an approximate algorithm:

1) Runs the cursor and write all the values ​​in ArrayList<String> 2) Then call the .size() method from the list to find out its length 3) If size > 0 , then we do the following

 Random random = new Random(); int randomValue = random.nextInt(size); String name = list.get(randomValue + 1); 

So everything should work out.