0

I'm creating a simple todo list. So, I have activity (NewTaskActivity) Xml code:

<?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>

And I have a class 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();
      }
   }
 }

In this class(NewTaskActivity.java) I want to add data in database. Database class (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);
    }
}

And my data don't insert into database. Why? I checked for data in NewTaskActivity.java using Log.d() after 'insert' and before 'finish' but they got me nothing. I've done todo list later but I have no such problems.

2

2 Answers 2

1

Your CREATE statement defines the column _id as INT NOT NULL PRIMARY KEY but not AUTOINCREMENT so you have to supply it yourself but you don't.
Of course it's better to make it AUTOINCREMENT:

_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE

Edit: uninstall the app from the emulator/device and then run the modified code, otherwise the changes won't affect your db because the onCreate() method of SQLOpenHelper() won't be triggered.

Sign up to request clarification or add additional context in comments.

Comments

0

Could you please change your _id to support AUTOINCREMENT and try again ? Because you are not passing any value to it and u defined it as primary key which means NOT NULL.

PS: you can remove "not null" from _id definition because primary key means not null.

_id INTEGER PRIMARY KEY AUTOINCREMENT

And dont forget to change your db version or reinstalling app to force db(database) recreate.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.