我正在创建一个简单的待办事项列表。所以,我有活动 (NewTaskActivity) Xml 代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/et_new_task_input"
android:hint="@string/new_task_input_hint"
android:textSize="15sp"
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fb_float_button_apply"
android:src="@drawable/ic_check"
app:fabSize="normal"
android:layout_margin="10dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</LinearLayout>
我有一个类NewTaskActivity.java:
public class NewTaskActivity extends AppCompatActivity {
private DBHelper dbHelper;
private SQLiteDatabase database;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_task_activity);
dbHelper = new DBHelper(NewTaskActivity.this);
database = dbHelper.getWritableDatabase();
FloatingActionButton floatingActionButton =
findViewById(R.id.fb_float_button_apply);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
addNewTask();
}
});
}
@Override
protected void onDestroy () {
super.onDestroy();
dbHelper.close();
}
@SuppressLint("SimpleDateFormat")
private void addNewTask () {
EditText task_input = findViewById(R.id.et_new_task_input);
if (!task_input.getText().toString().equals("")) {
ContentValues contentValues = new ContentValues();
contentValues.put("task", task_input.getText().toString());
contentValues.put("date",
new SimpleDateFormat("dd/MM/yymm:hh").
format(Calendar.getInstance().getTime()));
database.insert("tasks", null, contentValues);
finish();
}
}
}
在这个类(NewTaskActivity.java)中,我想在数据库中添加数据。数据库类(DBHelper.java):
public class DBHelper extends SQLiteOpenHelper {
private static final int DB_VERSION = 2;
private static final String DB_NAME = "simpletodo";
public DBHelper (@Nullable Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate (SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS tasks (_id INT NOT NULL PRIMARY
KEY, task TEXT NOT NULL, date TEXT NOT NULL)");
}
@Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS tasks");
onCreate(db);
}
}
而且我的数据不会插入到数据库中。为什么?我在“插入”之后和“完成”之前使用 Log.d()检查了NewTaskActivity.java中的数据,但他们什么也没给我。我后来完成了待办事项列表,但我没有这样的问题。
您的 CREATE 语句将列定义_id
为INT NOT NULL PRIMARY KEY
但不是, AUTOINCREMENT
因此您必须自己提供它,但您不需要。
当然最好是这样AUTOINCREMENT
:
_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE
编辑:从卸载模拟器/设备的应用程序,然后运行修改后的代码,否则这些修改将不会影响你的数据库,因为onCreate()
方法SQLOpenHelper()
不会被触发。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句