我正在创建一个Android应用程序并将其与数据库连接。 Android编程[新波士顿教程编号49-54]

时间:2016-09-13 14:29:58

标签: java android

我正在创建一个Android应用程序,并尝试将其与数据库连接。我在Youtube上关注TheNewBoston代码。我已成功创建了该应用。

但是当我运行它时,在插入查询中,我的应用程序挂起并且没有响应。

以下是代码:

MyDBHandler.java

public class MyDBHandler extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "newproducts.db";
public static final String TABLE_NAME = "products_table";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAME = "product_name";


public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String query = "CREATE TABLE " +TABLE_NAME+ "("+
            COLUMN_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
            COLUMN_NAME+ " TEXT "+
            ");";
    db.execSQL(query);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " +TABLE_NAME);
    onCreate(db);
}
public void add(Products products){
    ContentValues contentValues = new ContentValues();
    contentValues.put(COLUMN_NAME, products.get_productname());
    SQLiteDatabase db = getWritableDatabase();
    db.insert(TABLE_NAME,null,contentValues);
    db.close();
}

public String dbToString(){
    SQLiteDatabase db = getWritableDatabase();
    String dbString = "";
    String query = "SELECT * FROM "+TABLE_NAME+";";

    Cursor cursor = db.rawQuery(query,null);
    cursor.moveToFirst();

    while(!cursor.isAfterLast()){
        if(cursor.getString(cursor.getColumnIndex(""+COLUMN_NAME+""))!=null){
            dbString+= cursor.getString(cursor.getColumnIndex(""+COLUMN_NAME+""));
            dbString += "\n";
        }
    }
    db.close();
    return dbString;
}
}

MainActivity.Java

public class MainActivity extends AppCompatActivity {
EditText mkInput;
TextView  mkText;
MyDBHandler dbHandler;
public int dbVersion;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mkInput = (EditText)findViewById(R.id.mkInput);
    mkText = (TextView)findViewById(R.id.textView);
    dbVersion = MyDBHandler.DATABASE_VERSION;
    dbHandler = new MyDBHandler(this, null, null, dbVersion);
    printDatabase();
}

public void addClicked(View view){
    Products products =  new Products(mkInput.getText().toString());
    dbHandler.add(products);
    printDatabase();
}
public void deleteClicked(View view){
    Toast.makeText(MainActivity.this,"This is a toast",Toast.LENGTH_LONG).show();
}

public void printDatabase(){
    String dbString = dbHandler.dbToString();
    mkText.setText(dbString);
    mkInput.setText("");
}
}

Products.java

public class Products  {
private int _id;
private String _productname;

public Products(String _productname) {
    this._productname = _productname;
}

public void set_id(int _id) {
    this._id = _id;
}

public void set_productname(String _productname) {
    this._productname = _productname;
}

public String get_productname() {
    return _productname;
}

public int get_id() {
    return _id;
}
}

activity_main.xml中

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/mkInput"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:width="300dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Add"
    android:id="@+id/addButton"
    android:layout_below="@+id/mkInput"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="48dp"
    android:onClick="addClicked"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Delete"
    android:id="@+id/deleteButton"
    android:layout_alignBottom="@+id/addButton"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:onClick="deleteClicked"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Default Text"
    android:id="@+id/textView"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

输出屏幕 This is the app interface

点击AddButton,应用程序挂起

1 个答案:

答案 0 :(得分:0)

将您的dbToString代码更改为此

public String dbToString(){
    SQLiteDatabase db = getWritableDatabase();
    String dbString = "";
    String query = "SELECT * FROM "+TABLE_NAME+";";
Cursor cursor = db.rawQuery(query,null);
if(cursor.moveToFirst()){

do{
    if(cursor.getString(cursor.getColumnIndex(""+COLUMN_NAME+""))!=null){
        dbString+= cursor.getString(cursor.getColumnIndex(""+COLUMN_NAME+""));
        dbString += "\n";
    }
}while(cursor.moveToNext());
}

db.close();
return dbString;
}

基本上app现在正在进行自我循环。

请注意moveToNext() 的区别。它会将光标移动到下一个位置,而isAfterLast只是告诉 光标是否在最后一个结果之后see