Всем привет!
Только познаю SQLite.
Подскажите пожалуйста, в чем может быть проблема - пока нахожусь в самом приложение - чтение и запись проходят успешно. Как только вышел из эмулятора и зашел, пытаюсь прочесть запись в БД выдает ссылку ,что проблема в Курсоре. Так понимаю, что в БД не сохранилась запись?
Разметка
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="385dp"
android:layout_height="70dp"
android:orientation="horizontal"
android:weightSum="4"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="@+id/btnAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="AddDB"
android:text="Запись"></Button>
<Button
android:id="@+id/btnUpdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="UpdateDB"
android:text="Обновить"></Button>
<Button
android:id="@+id/btnRead"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="ReadfromDB"
android:text="Читать"></Button>
<Button
android:id="@+id/btnClear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="DeleteDB"
android:text="Очистить"></Button>
</LinearLayout>
<LinearLayout
android:layout_width="233dp"
android:layout_height="161dp"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@+id/linearLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="@+id/scoreText"
android:hint="Очки"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<EditText
android:id="@+id/lvlText"
android:hint="Уровень"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<EditText
android:id="@+id/tryText"
android:hint="Попыток"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
</LinearLayout>
<LinearLayout
android:layout_width="264dp"
android:layout_height="160dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout">
<TextView
android:id="@+id/scoreView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp" />
<TextView
android:id="@+id/lvlView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp" />
<TextView
android:id="@+id/tryView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Мейн
package freijer.app.sucktest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.renderscript.Sampler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
Button btnAdd, btnRead, btnClear;
EditText scoreText, lvlText, tryText;
TextView scoreView, lvlView, tryView;
String addsc;
String addlvl;
String addtryss;
String score;
String lvl;
String trys;
DataHelper dbHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAdd = findViewById(R.id.btnAdd);
btnRead = findViewById(R.id.btnRead);
btnClear = findViewById(R.id.btnClear);
scoreText = findViewById(R.id.scoreText);
lvlText = findViewById(R.id.lvlText);
tryText = findViewById(R.id.tryText);
scoreView = findViewById(R.id.scoreView);
lvlView = findViewById(R.id.lvlView);
tryView = findViewById(R.id.tryView);
score = scoreText.getText().toString();
lvl = lvlText.getText().toString();
trys = tryText.getText().toString();
dbHelper = new DataHelper(this);
}
public void AddDB(View v) {
addsc = scoreText.getText().toString();
addlvl = lvlText.getText().toString();
addtryss = tryText.getText().toString();
dbHelper.WriteDB(addsc, addlvl, addtryss);
}
public void ReadfromDB(View v) {
dbHelper.ReadDB();
}
public void DeleteDB(View v){
dbHelper.DeleteDB();
}
public void UpdateDB(View v){
addsc = scoreText.getText().toString();
addlvl = lvlText.getText().toString();
addtryss = tryText.getText().toString();
dbHelper.UpdateDB(addsc, addlvl, addtryss);
}
}
DBOpenHelper
package freijer.app.sucktest;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DataHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "contactDb";
public static final String TABLE_NAME = "contacts";
public static final String KEY_ID = "_id";
public static final String KEY_SCORE = "scores";
public static final String KEY_LVL = "lvls";
public static final String KEY_TRYS = "tryss";
SQLiteDatabase database;
ContentValues contentValues = new ContentValues();
public DataHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + "(" + KEY_ID
+ " integer primary key," + KEY_SCORE + " text," + KEY_LVL+ " text," + KEY_TRYS + " text" + ");");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists " + TABLE_NAME);
onCreate(db);
}
public void WriteDB(String score, String lvl, String tryss){
database = getWritableDatabase();
contentValues.put(KEY_SCORE, score);
contentValues.put(KEY_LVL, lvl);
contentValues.put(KEY_TRYS, tryss);
database.insert(TABLE_NAME, null, contentValues);
}
public void ReadDB(){
Cursor cursor = database.query(TABLE_NAME, null, null, null, null, null, null);
if (cursor.moveToFirst()) {
int idIndex = cursor.getColumnIndex(KEY_ID);
int scoreIndex = cursor.getColumnIndex(KEY_SCORE);
int lvlIndex = cursor.getColumnIndex(KEY_LVL);
int tryslIndex = cursor.getColumnIndex(KEY_TRYS);
do {
Log.d("myLogs", "ID = " + cursor.getInt(idIndex) +
", очки = " + cursor.getString(scoreIndex) +
", уровень = " + cursor.getString(lvlIndex) +
", попыток = " + cursor.getString(tryslIndex) );
} while (cursor.moveToNext());
} else
Log.d("myLogs","0 rows");
cursor.close();
}
public void DeleteDB(){
database.delete(TABLE_NAME, null, null);
database.close();
}
public void UpdateDB(String score, String lvl, String tryss){
database = getWritableDatabase();
contentValues.put(KEY_SCORE, score);
contentValues.put(KEY_LVL, lvl);
contentValues.put(KEY_TRYS, tryss);
String id = "1";
int updCount = database.update(TABLE_NAME, contentValues, KEY_ID + "= ?" , new String[] {id});
Log.d("myLogs", "updated rows count = " + updCount);
}
}