D
D
dnpgk2019-06-09 13:23:43
Java
dnpgk, 2019-06-09 13:23:43

Error when using do while to display random records from db?

When requesting from the database in TextView (text1) the text of the question, after the answer in the EditText, display the next random line of the question, while taking into account the number of correct and incorrect answers through numerical variables ("a" and "x"). Wrote the following code. For some reason, after the answer, it does not load the text of the next question (in rawQuery, product is the text of the question, and gono is the correct answer stored in the database, which is compared with the entered value through OnKeyListener). Tell me, please, where you stumbled.

import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;


public class MainActivity extends AppCompatActivity  {

    //Объявим переменные компонентов
    EditText editText;
    TextView text1;

    private DatabaseHelper mDBHelper;
    private SQLiteDatabase mDb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDBHelper = new DatabaseHelper(this);



        try {
            mDBHelper.updateDataBase();
        } catch (IOException mIOException) {
            throw new Error("UnableToUpdateDatabase");
        }

        try {
            mDb = mDBHelper.getWritableDatabase();
        } catch (SQLException mSQLException) {
            throw mSQLException;
        }

        //Найдем компоненты в XML разметке

        text1 = (TextView) findViewById(R.id.text1);
        editText =  findViewById(R.id.editText);
        final int[] a = {0};
        final int[] x = {0};
        do {


            String product = "";
            String gono = "";


            final Cursor cursor = mDb.rawQuery("SELECT  * FROM " + "book1" + " ORDER BY RANDOM() LIMIT 1", null);
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                product += cursor.getString(1);
                gono += cursor.getString(2);
                cursor.moveToNext();
            }
            cursor.close();

            text1.setText(product);


            final String finalGono = gono;

            editText.setOnKeyListener(new View.OnKeyListener() {

                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    if (event.getAction() == KeyEvent.ACTION_DOWN &&
                            (keyCode == KeyEvent.KEYCODE_ENTER)) {
                        if (editText.getText().toString().equals(finalGono))
                            a[0]++;
                        else
                            x[0]++

                    }
                    return false;
                }


            });
        }  while (a[0] > 2*x[0]);


    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question