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

activity.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;
}


数据库处理程序

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();
        }
});

最佳答案

这听起来是错误的,但不要db.close。随后对getWritableDatabase的调用将返回相同的,已经关闭的对象。如果使用了相同的基础现在关闭的对象,它也可能影响您从数据库读取的能力。

从仿真器中拉出db文件,并在SQLite编辑器中将其打开。http://sqliteadmin.orbmu2k.de/可以使用,但是如果可以找到更好的文件,请使用它。

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

同样出于调试目的,请尝试使用insertOrThrow而不是insert进行操作,如果插入失败而不是静默失败,您将获得异常。

10-08 06:15
查看更多