Я пытаюсь создать приложение SQLite, где я могу добавлять и удалять строки, и оно должно быть напечатано сразу.
Эмулятор зависает, когда я нажимаю одну из кнопок.
У меня нет ошибок, logcat ничего не показывает.
Я пробовал его на эмуляторе и на реальном устройстве, и он показывает тот же результат.
Это мой MainActivity без импорта:
public class MainActivity extends AppCompatActivity {
//in the tutorial these variables were not private
private EditText et1;
private TextView tv1;
private MyDBHandler dbHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.editText);
tv1 = (TextView) findViewById(R.id.textView);
dbHandler = new MyDBHandler(this, null, null, 1);
}
public void insertButtonClicked(View view) {
Products product = new Products(et1.getText().toString());
dbHandler.addProduct(product);
printDatabase();
}
public void deleteButtonClicked(View view) {
String inputText = et1.getText().toString();
dbHandler.deleteProduct(inputText);
printDatabase();
}
public void printDatabase() {
String dbString = dbHandler.databaseToString();
tv1.setText(dbString);
et1.setText("");
}
}
Класс Продукты:
public class Products {
private int _id;
private String _productname;
public Products(String productname) {
this._productname = productname;
}
public int get_id() {
return _id;
}
public String get_productname() {
return _productname;
}
public void set_id(int _id) {
this._id = _id;
}
public void set_productname(String productname) {
this._productname = productname;
}
}
Класс myDBHandler без импорта:
public class MyDBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "products.db";
public static final String TABLE_PRODUCTS = "products";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_PRODUCTNAME = "_productname";
public MyDBHandler(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_PRODUCTS + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_PRODUCTNAME + " TEXT" +
");";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
onCreate(db);
}
//add a new row to the database
public void addProduct(Products product) {
ContentValues values = new ContentValues();
values.put(COLUMN_PRODUCTNAME, product.get_productname());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_PRODUCTS, null, values);
}
//delete a row from the database
public void deleteProduct(String productName) {
SQLiteDatabase db = getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME +
" =\"" + productName + "\";");
}
//print out the database as a string
public String databaseToString() {
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
//the tutorial guy did a "WHERE 1" instead of the ";" but that also does not work
String query = "SELECT * FROM " + TABLE_PRODUCTS + ";";
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
while(!c.isAfterLast()) {
if(c.getString(c.getColumnIndex("_productname")) != null) {
dbString += c.getString(c.getColumnIndex("_productname"));
dbString += "\n";
}
}
c.close();
db.close();
return dbString;
}
}
Наиболее важные части файла xml:
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:hint="@string/edittexthint"
android:importantForAutofill="no"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:onClick="insertButtonClicked"
android:text="@string/insert_msg"
app:layout_constraintStart_toStartOf="@+id/editText"
app:layout_constraintTop_toBottomOf="@+id/editText" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:onClick="deleteButtonClicked"
android:text="@string/delete_msg"
app:layout_constraintEnd_toEndOf="@+id/editText"
app:layout_constraintTop_toBottomOf="@+id/editText" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="@string/tv_content"
app:layout_constraintTop_toBottomOf="@+id/button"
app:layout_constraintStart_toStartOf="@+id/button"/>
</androidx.constraintlayout.widget.ConstraintLayout>