Search String Random in Sqlite and then show the Result on the Screen with button

Asked

Viewed 56 times

0

// Create Database. final Sqlitedatabase bankDados = openOrCreateDatabase("Table name", MODE_PRIVATE, null);

    // Criar Tabela.

    bancoDados.execSQL("CREATE TABLE IF NOT EXISTS  questions(id INT(3),perguntas VARCHAR)");

    // Inserir Dados. ( Crl+d para duplicar INSERT INTO ).

   // bancoDados.execSQL("INSERT INTO questions(id,perguntas) VALUES (1,'Posso ser cromado 1')");
   // bancoDados.execSQL("INSERT INTO questions(id,perguntas) VALUES (2,'Posso ser cromado 2')");


    // Puxar os dados da Tabela.

    bt_perguntar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

    Cursor cursor = bancoDados.rawQuery("" +
                    "SELECT perguntas " +
                    "FROM questions " +
                    "ORDER BY RANDOM() " +
                    "LIMIT 1;",
            null);


    int indicePerguntas = cursor.getColumnIndex("perguntas");

    cursor.moveToFirst();
    while (cursor != null){

        text_pergunta.setText(cursor.getString(indicePerguntas));
        cursor.moveToNext();

        //Log.i("RESULTADO - Pergunta ",cursor.getString(indicePerguntas));
    }

        }
    }); 

Log error.

inserir a descrição da imagem aqui

The idea is to work like this: inserir a descrição da imagem aqui

  • Do not put the code in image. Then edit your question and enter the code in text.

  • Andrei found it simpler, I’m new here. Would it be so I could copy the code and test? and Get an answer?

  • Exact Guil! This way you also increase your chances of someone responding to you! Hug!

1 answer

1


Just wear it inside the Cursor that one query that will work and you can use the cursor:

    Cursor cursor = bancoDados.rawQuery("" +
            "SELECT perguntas " +
            "FROM questions " +
            "ORDER BY RANDOM() " +
            "LIMIT 1;", 
            null);
  • Andrei, good morning. It worked perfectly as I wanted. I stayed one day cracking my head. Knowledge is power. Really worth it.

  • @Guildraco tranquil. Don’t forget to approve ! There is a sign in the reply for you to approve. Sits at the top of the left side of the response, below the sign of UP and DOWN.

  • Not to abuse, Rsrs.. Is there a topic here that shows how I could display this selected text on the project screen by clicking the button? I tried, but then I made fun of everything.Using text_question.setText(cursor.getString(indicePerguntas); cursor.moveToNext(); instead of log. it arrow the text but only when it closes and opens the app. I wanted to add this to the click event. Fight, hugs. Already saved me. Rsrsr

  • @Guildraco then, I don’t quite understand what you want... Could you explain me better?

  • @Guildraco I just saw... So... you better insert the error that appears in the log. Because there may be several reasons for this to happen. It is impossible to detect the error just with the code you passed as an answer. Put the error in the question! And then delete the answer you posted ok?

  • Thanks Andrei, I think they already deleted it, I didn’t know how to delete it, Rsrs. Ta on the count here to delete it, something like that. I updated with the log error. and the section of Cód. and what I’m wanting. Fight for strength, and already helped a lot with the question of random setar relaxes. Rss

  • @Guildraco tranquilo amigo! = ) Then check out this answer > https://stackoverflow.com/a/5316390/9303032 your problem is the same.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.