-1

I am trying to create a TaskManager Where I have a setDate Button, When I click on SetDate Button it passed me on next Activity where I placed a DatePicker in the layout. I am trying to set date and then passing that set date into the database using OK button.

Datepicker Activity:

public class Task_Details extends Activity implements OnClickListener {

DatePicker datePicker;
private DatePicker.OnDateChangedListener dateSetListener;
Button okBtn, cancelBtn;
String setDate;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.task_details);
    datePicker = (DatePicker)findViewById(R.id.setDueDate);
    datePicker.init(datePicker.getYear(),datePicker.getMonth(), datePicker.getDayOfMonth(), dateSetListener);

okBtn = (Button)findViewById(R.id.okBtn);
    okBtn.setOnClickListener(this);
    cancelBtn = (Button)findViewById(R.id.cancelBtn);
    cancelBtn.setOnClickListener(this);
@Override
public void onClick(View v) {
    switch(v.getId()){
    case R.id.okBtn:
        String returnDate = setDate();
        db = new TodoTask_Database(getApplicationContext());  //Database call
        db.addTaskDetails(returnDate);   //Datebase method
        break;
    case R.id.cancelBtn:
        Intent intent = new Intent(getApplicationContext(), Add_Task.class);
        startActivity(intent);
        break;
    }
}
    String setDate(){
    dateSetListener = new DatePicker.OnDateChangedListener() {
        public void onDateChanged(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {

            System.out.println(+ year+"-"+ monthOfYear+"-"+ dayOfMonth);

            String Year=Integer.toString(year); 
            String Month=Integer.toString(monthOfYear); 
            String Day=Integer.toString(dayOfMonth);

            System.out.println(Year+"-"+Month+"-"+Day); 
        }
    };
    return setDate;
    }
}

I am trying to get date and save into the Database.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Shweta
  • 1,145
  • 5
  • 18
  • 35

5 Answers5

0

The problem that you have here, is that you are trying to get a result from the picker by setting a callback listener.

What you need to do, is set the listener in your onCreate method after findViewById.

When a date is selected, set a member variable. When you click OK, use the member variable in your database method.

Knossos
  • 15,802
  • 10
  • 54
  • 91
0

try it.

protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_myprofile);
    datePickerDialog = new DatePickerDialog(this, mDateSetListener, year,
                month, day);
String returnDate = ""+date;
}
DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
                date = new Date(year - 1900, monthOfYear, dayOfMonth);

        }

    };
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
nilesh patel
  • 834
  • 1
  • 5
  • 10
0

You can do something like this:

 case R.id.okBtn:
      String year = datePicker.getYear(); //and so on!
      ...
      String date = year + month + day;
      db.addToDb(date);
      break;
Semyon Danilov
  • 1,753
  • 1
  • 17
  • 37
0

Please try to use bellow code.

package com.phone.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.phone.util.ConstantLib;

public class DBAdapter {

    private static final String TAG = "DBAdapter";
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";  
        private static final String KEY_DATE = "dates";
    private static final String DATABASE_NAME = "SqlDB.db";
    private static final String DATABASE_TABLE = "tab";
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_VERSION = 1;
    private static final String TABLE_CREATE = "create table  if not exists "
            + "tab(" + "id integer primary key not null, "
            + "name text not null , " +

            "dates text not null);";

    private final Context context;

    private final DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx) {
        context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper {
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            try {
                db.execSQL(TABLE_CREATE);
            } catch (Exception e) {
                Log.i(TAG, e.toString());
            }
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            try {
                db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
                onCreate(db);
            } catch (Exception e) {
                Log.i(TAG, e.toString());
            }
        }
    }

    // end DatabaseHelper

    // ---opens the database---
    public DBAdapter open() throws SQLException {
        try {
            db = DBHelper.getWritableDatabase();
            db.setLockingEnabled(true);// -----------------------------------
        } catch (Exception e) {
            Log.i(TAG, e.toString());
        }
        return this;
    }

    // ---closes the database---
    public void close() {
        try {
            DBHelper.close();

        } catch (Exception e) {
            Log.i(TAG, e.toString());
        }
    }

    // ---insert a remindar into the database---
    public long insertData(String id, String name, String date) {
        try {
            ContentValues initialValues = new ContentValues();
            initialValues.put(KEY_ID, id);
            initialValues.put(KEY_NAME, name);
            initialValues.put(KEY_DATE, date);

            return db.insert(DATABASE_TABLE, null, initialValues);
        } catch (SQLiteConstraintException e) {

        }

        catch (Exception e) {
            Log.i(TAG, e.toString());
        }

        return -1;
    }


}
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
nilesh patel
  • 834
  • 1
  • 5
  • 10
0
Date d = new Date();
CharSequence s  = DateFormat.format("MM-dd-yy hh-mm-ss", d.getTime());
Log.d("Current date and time",s.toString());
Kirit Vaghela
  • 12,572
  • 4
  • 76
  • 80