Began to deal with SQLite in Android. Understood how to find out the amount of data in the cursor, but did not understand how to display this data:

userCursor = db.rawQuery("select " + COLUMN_NAME + " from " + TABLE, null);//Получаем все данные из таблицы TABLE(users) textBD.setText(Integer.toString(userCursor.getCount()));// В getCount получили сколько записей содержит курсор userCursor.moveToFirst();//Перевели курсор к первому значению 

Full code:

 @Override public void onClick(View v) { userCursor = db.rawQuery("select " + COLUMN_NAME + " from " + TABLE, null);//Получаем все данные из таблицы TABLE(users) textBD.setText(Integer.toString(userCursor.getCount()));// В getCount получили сколько записей содержит курсор userCursor.moveToFirst();//Перевели курсор к первому значению if (userCursor.moveToFirst()) { while (!userCursor.isAfterLast()) { String data = userCursor.getString(userCursor.getColumnIndex("data")); // обрабатываем data // двигаемся к следующему значению userCursor.moveToNext(); } } // не забывайте закрыть курсор userCursor.close(); Toast toast = Toast.makeText(getApplicationContext(), "ОКЕЙ", Toast.LENGTH_SHORT); toast.show(); } }); } 

Errors:

 Process: com.example.mysql_legkosuka, PID: 10864 java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. at android.database.CursorWindow.nativeGetString(Native Method) at android.database.CursorWindow.getString(CursorWindow.java:438) at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51) at com.example.mysql_legkosuka.MainActivity$1.onClick(MainActivity.java:51) at android.view.View.performClick(View.java:4756) at android.view.View$PerformClick.run(View.java:19749) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

    2 answers 2

    To get the data from the cursor you need to cycle through it:

     if (userCursor.moveToFirst()) { while (!userCursor.isAfterLast()) { String data = userCursor.getString(userCursor.getColumnIndex("data")); // обрабатываем data // двигаемся к следующему значению userCursor.moveToNext(); } } // не забывайте закрыть курсор userCursor.close(); 
    • In edits added the full code, and errors. - Romag
    • one
      I gave an example of code, and not a solution to your problem, only you should deal with this. Please first read the SQLite documentation on Android and learn more about Java programming. - Egor Smolyakov
    • one
      @Romag you can see that there is no check for the fact that there is data in your table (query), you need to see for yourself if there is data there and not read from empty) - Evgeny Suetin

    If you use a list, then you can use SimpleCursorAdapter - well, probably for a simple example.

     scAdapter = new SimpleCursorAdapter(this, R.layout.item, cursor, from, to); lvData = (ListView) findViewById(R.id.lvData); lvData.setAdapter(scAdapter); 

    If you want to do something special, then RecyclerView + Cursor is at your disposal, but then you have to keep track of changes, etc. (not the easiest task).