Я создаю простой список задач.Итак, у меня есть XML-код активности (NewTaskActivity):
<?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.Ява) Я хочу добавить данные в базу данных.Класс базы данных (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);
}
}
И мои данные не вставляются в базу данных.Зачем?Я проверил данные в NewTaskActivity.java , используя Log.d () после 'вставки' и до 'окончания', но они мне ничего не дали.Я сделал список задач позже, но у меня нет таких проблем.