-4

This is the onClick method of HomeActivity file:

  public void onClick(View v) {

    Task=task.getText().toString();

    datee=date.getText().toString();

    boolean c= db.insertValues(Task,datee);

    if(c==true) {
        Toast.makeText(HomeActivity.this,"success",Toast.LENGTH_LONG).show();
    }
    else if(c==false) {
        Toast.makeText(HomeActivity.this,"failed",Toast.LENGTH_LONG).show();
    }
   Intent intent=new Intent(HomeActivity.this,DisplayActivity.class);
    startActivity(intent);
}

And this is the sqlitehelper java class for creating database and storing values:

public class DatabaseHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME="AppDB";

    public static final int DATABASE_VERSION=1;

    public static final String TABLE_NAME="Users";

    public static final String Column1="id";

    public static final String Column2="task";

    public static final String Column4="ddate";

    public static final String Query="CREATE TABLE "+TABLE_NAME+"("+Column1+" NUMBER NOT NULL AUTO INCREMENT,"+Column2+" TEXT NOT NULL,"+Column3+" TEXT NOT NULL)";

    public DatabaseHelper(Context context) {

        super(context,DATABASE_NAME,null,DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(Query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
    public boolean insertValues(String task,String daate) {
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(Column2,task);
        values.put(Column3,daate);
        long rows=db.insertOrThrow(TABLE_NAME,null,values);
        if(rows>0){
            return true;
        }
        else
            return false;
    }
     public Cursor retrieveValues() {
        SQLiteDatabase db=this.getReadableDatabase();
        Cursor cr=db.rawQuery("select "+Column2+","+Column3+","+Column4+" from "+TABLE_NAME,null);
        return cr;
    }
}

another activity displayActivity for retrieving values-

    public class DisplayActivity extends AppCompatActivity {
    ListView tasklist;
    DatabaseHelper db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show);
    tasklist=(ListView)findViewById(R.id.taskList);
    db=new DatabaseHelper(this);
    Cursor cursor=db.retrieveValues();
    CustomAdapter todoadapter=new CustomAdapter(this,cursor);
    tasklist.setAdapter(todoadapter);
}

}

and the CustomAdapter class is as-

    public class CustomAdapter extends CursorAdapter {
    TextView task,daate;
    public CustomAdapter(Context context, Cursor cursor) {
    super(context, cursor, 0);
}

    // The newView method is used to inflate a new view and return it,
    // you don't bind any data to the view at this point.
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.adapter, parent, 
    false);
}

    // The bindView method is used to bind all data to a given view
    // such as setting the text on a TextView.
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
    // Find fields to populate in inflated template
    task=(TextView)view.findViewById(R.id.dynamicTask);
    daate=(TextView)view.findViewById(R.id.dynamicDate);
    String Task=cursor.getString(cursor.getColumnIndex("task"));
    String Daate=cursor.getString(cursor.getColumnIndex("ddate"));
    task.setText(Task);
    daate.setText(Daate);
}

}

My Application is crashing when I click on save button,the values are storing in database but when i retrieve the values it shows an exception -

  10-15 17:54:13.395 4139-4139/com.example.bestbuy.mytry E/AndroidRuntime: 
  FATAL
  EXCEPTION: main
                                                                     Process: com.example.bestbuy.mytry, PID: 4139
                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bestbuy.mytry/com.example.bestbuy.mytry.DisplayActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                                                                         at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:135)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at java.lang.reflect.Method.invoke(Method.java:372)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
                                                                      Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
                                                                         at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
                                                                         at android.widget.CursorAdapter.init(CursorAdapter.java:172)
                                                                         at android.widget.CursorAdapter.<init>(CursorAdapter.java:149)
                                                                         at com.example.bestbuy.mytry.CustomAdapter.<init>(CustomAdapter.java:19)
                                                                         at com.example.bestbuy.mytry.DisplayActivity.onCreate(DisplayActivity.java:18)
                                                                         at android.app.Activity.performCreate(Activity.java:6033)
                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                                                                         at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:135) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697) 

I am not able to interpret the exception. if u want some more details let me know! please help me in correcting it.. thank you

Suraj
  • 41
  • 7

1 Answers1

0

You can use a text datatype to store dates within SQLite while table creation.

Storing dates in UTC format, the default if you use datetime('now') (yyyy-MM-dd HH:mm:ss) will then allow sorting by the date column.

Retrieving dates as strings from SQLite table you can then convert them as required into formats using the Calendar or the DateUtils.formatDateTime method.

Pritesh Patel
  • 678
  • 17
  • 35
  • Thanks for your answer but how can i then extract date and time from this: (yyyy-MM-dd HH:mm:ss) – Suraj Sep 27 '17 at 15:04
  • 2
    After getting date text from sqlite convert to Date type using iso8601Format.parse(textDate), then use SimpleDateFormat to extract Date and Time separately. – Pritesh Patel Sep 27 '17 at 15:12
  • kindly view the code again, i made some changes in it and also mentioned error stack. thank you – Suraj Oct 15 '17 at 12:39