1

我是 android 新手,我正在开发一个将用户输入保存到数据库的应用程序。但是,由于某些原因,我看不到用户按下“保存”按钮时所保存的数据。你能帮我保存这些代码中的值吗?

活动.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Spinner 
        android:id="@+id/spn_District"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Spinner 
        android:id="@+id/spn_Province"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Spinner 
        android:id="@+id/spn_InfoType"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView 
        android:id="@+id/txt_Date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView 
        android:id="@+id/txt_Remarks"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

Cons_IReport.java(构造函数)

public Cons_iReport(String district, String province, String infoType, String rDateObserved, String rRemarks){
    this._district = district;
    this._province = province;
    this._infoType = infoType;
    this._dateObserved = rDateObserved;
    this._remarks = rRemarks;
}

数据库处理程序.java

public void SaveReport(Cons_iReport save){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(Constants.REPORT_DISTRICT, save.getDistrict()); 
    values.put(Constants.REPORT_PROVINCE, save.getProvince()); 
    values.put(Constants.REPORT_INFOTYPE, save.getInfoType()); 
    values.put(Constants.REPORT_DATEOBSERVED, save.getDateObserved()); 
    values.put(Constants.REPORT_REMARKS, save.getRemarks());

    db.insert(Constants.TABLE_REPORT, null, values);
    db.close(); 
}

MainActivity.java

btn_Save.setOnClickListener(new OnClickListener() {

    @Override
        public void onClick(View arg0) {
            String rDistrict = spn_District.getSelectedItem().toString();
            String rProvince = spn_Province.getSelectedItem().toString();
            String rInfoType = spn_InfoType.getSelectedItem().toString();
            String rDateObserved = txt_Date.getText().toString();
            String rRemarks = txt_Remarks.getText().toString();

            databaseHandler.SaveReport(new Cons_iReport(rDistrict, rProvince, rInfoType, rDateObserved, rRemarks));
            Toast.makeText(getApplicationContext(), "SAVED!\n" + rDistrict + "\n" + rProvince + "\n" + rInfoType +  "\n" + rDateObserved + "\n" + rRemarks, Toast.LENGTH_SHORT).show();
        }
}); 
4

1 回答 1

2

这听起来不对,但不要db.close。对 getWritableDatabase 的后续调用将返回相同的、已关闭的对象。如果使用相同的底层现已关闭的对象,它也可能会影响您从数据库中读取的能力。

从模拟器中拉出 db 文件并在 SQLite 编辑器中打开它http://sqliteadmin.orbmu2k.de/可以,但如果你能找到更好的使用它。

该数据库的表中是否有任何行?

同样出于调试目的,请尝试使用 insertOrThrow 而不是 insert 如果插入失败而不是静默失败,则会出现异常。

于 2013-07-04T00:48:43.410 回答