Я разрабатываю пример приложения для Android с подключением базы данных sqlite. Я не вижу реального интерфейса из-за ошибки отображения. Не отображать ничего для этого и некоторое время отображения также не отвечает. Это не отображает никаких ошибок, но Android-устройство отображает изображение
, также я добавляю свой код ниже.
Myactivity.java класс
package com.example.randikawann.androidconnectsqlite;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText userInput;
TextView userText;
MyDBHandler myDBHandler;
private static final String TAG = "MyActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userInput = (EditText) findViewById(R.id.user_input);
userText = (TextView) findViewById(R.id.user_text);
myDBHandler=new MyDBHandler(this,null,null,1);
printDatabase();
Log.i(TAG, "start application");
}
public void addButtonClicked(View view){
Products products=new Products(userInput.getText().toString());
myDBHandler.addProduct(products);
printDatabase();
Log.i(TAG, "add button clicked");
}
public void deleteButtonClicked(View view){
String inputText=userInput.getText().toString();
myDBHandler.deleteproduct(inputText);
printDatabase();
Log.i(TAG, "delete button clicked");
}
private void printDatabase() {
String dbString =myDBHandler.databaseToString();
userText.setText(dbString);
userInput.setText("");
Log.i(TAG, "print database");
}
@Override
protected void onDestroy() {
myDBHandler.close();
super.onDestroy();
}
}
это класс MyDBHandler.java
package com.example.randikawann.androidconnectsqlite;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDBHandler extends SQLiteOpenHelper{
private static final int DATABASE_VERSION=1;
private static final String DATABASE_NAME="products.db";
private static final String TABLE_PRODUCTS="products";
private static final String COLUMN_ID="id";
private static final String COLUMN_PRODUCTNAME="productname";
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){
super(context, DATABASE_NAME, factory,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 database
public void addProduct(Products products){
SQLiteDatabase db=getWritableDatabase();
ContentValues values=new ContentValues();
values.put(COLUMN_PRODUCTNAME,products.get_productname());
db.insert(TABLE_PRODUCTS,null,values);
db.close();
}
//delect a row in database
public void deleteproduct(String productName){
SQLiteDatabase db=getWritableDatabase();
String query="DELETE FROM "+ TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + "=\"" + productName +"\";" ;
db.execSQL(query);
}
//printout database as a string
public String databaseToString(){
String dbString = "";
SQLiteDatabase db = this.getWritableDatabase();
String query="SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1";
//cursor point to a location in your result
Cursor c=db.rawQuery(query, null);
//move to first row in your results
c.moveToFirst();
while (!c.isAfterLast()){
if (c.getString(c.getColumnIndex("productname"))!=null){
dbString +=c.getString(c.getColumnIndex("productname"));
dbString +="\n";
}
}
db.close();
return dbString;
}
}
Products.java класс
package com.example.randikawann.androidconnectsqlite;
public class Products {
private int _id;
private String _productname;
public Products() {
}
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;
}
}
также я добавляю вызов XML-файла в качестве действия main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/user_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_marginStart="150dp"
android:layout_marginTop="250dp"
android:text="Hello World!"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/add_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginStart="50dp"
android:layout_marginTop="150dp"
android:onClick="addButtonClicked"
android:text="Add"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/delete_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="150dp"
android:onClick="deleteButtonClicked"
android:text="Delete"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/user_input"
android:layout_width="262dp"
android:layout_height="wrap_content"
android:layout_marginLeft="62dp"
android:layout_marginStart="62dp"
android:layout_marginTop="86dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>