如何从数据库中的datepicker检索日期

时间:2014-01-22 15:04:16

标签: android datepicker

如何从数据库中的DatePicker检索数据

我想从数据库中检索DatePicker。 这是为了确保我输入的日期与数据相同,然后它将显示下一页。

我存储在数据库中的DatePicker是String。 我通过谷歌阅读,并了解SQLite不允许日期。

如果我要检索数据(日期)以检查输入,

我该如何检索和检查它? 有人可以帮忙吗?

dbadapter.java

public class DBAdapter {



    public static final String KEY_ROWID = "_id";
    public static final String KEY_DATE = "date";
    public static final String KEY_PRICE = "fuelprice";
    public static final String KEY_FUEL = "fuelpump";
    public static final String KEY_COST = "tcost";
    public static final String KEY_ODM = "odometer";
    public static final String KEY_CON = "fcon";

    private static final String TAG = "DBAdapter";

    private static final String DATABASE_NAME = "MyDB";
    private static final String DATABASE_TABLE = "fuelLog";
    private static final int DATABASE_VERSION = 2;



    private static final String DATABASE_CREATE =
            "create table fuelLog (_id integer primary key autoincrement, " + "date text not null, fuelprice text not null, fuelpump text not null, tcost text not null, odometer text not null, fcon text not null);";


        private final Context context;    

        private DatabaseHelper DBHelper;
        private SQLiteDatabase db;

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

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

            public void onCreate(SQLiteDatabase db) 
            {
                try{
                    db.execSQL(DATABASE_CREATE);    
                }catch (SQLException e){
                    e.printStackTrace();
                }
            }//onCreate

            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");
                db.execSQL("DROP TABLE IF EXISTS contacts");
                onCreate(db);
            }//onUpgrade

        }//DatabaseHelper


        public DBAdapter open() throws SQLException 
        {
            db = DBHelper.getWritableDatabase();
            return this;
        }//open


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


        //---insert a log into the database---
        public long insertLog(String date, String fuelprice, String fuelpump,String tcost,String odometer,String fcon ) 
        {
            ContentValues initialValues = new ContentValues();
            initialValues.put(KEY_DATE, date);
            initialValues.put(KEY_PRICE, fuelprice);
            initialValues.put(KEY_FUEL, fuelpump);
            initialValues.put(KEY_COST, tcost);
            initialValues.put(KEY_ODM, odometer);
            initialValues.put(KEY_CON, fcon);


            return db.insert(DATABASE_TABLE, null, initialValues);
        }//insertLog





 public Cursor getDateLog()
        {
        return db.query(DATABASE_TABLE, new String[] { KEY_DATE}, null, null, null, null, null);
        } 




            }

selection.java

 public class selection extends Activity {


    Button newButton;
    Button updateButton;
    Button deleteButton;
    Button summaryButton;
    static EditText updateEdit;
    private int mYear;
    private int mMonth;
    private int mDay;

    static final int DATE_DIALOG_ID = 0;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.selection);

        final DBAdapter dbAdaptor = new DBAdapter(getApplicationContext());
        final Cursor cursor = null;

        updateEdit = (EditText)findViewById(R.id.updateEdit);


        updateEdit.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                   // showDialog(DATE_DIALOG_ID);
                    DialogFragment newFragment = new DatePickerFragment();
                    newFragment.show(getFragmentManager(), "datePicker");
                }
            });



        newButton = (Button) findViewById(R.id.newBTN);
         newButton.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {

                 Intent mainAct=new Intent(getApplicationContext(),MainActivity.class);
                 startActivity(mainAct);

            }

            });












         updateButton = (Button) findViewById(R.id.updateBTN);
         updateButton.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {

                dbAdaptor.open();
                cursor  = dbAdaptor.getDateLog();
                if (updateEdit != null && updateEdit == (KEY_DATE)  )
                {
        Toast.makeText(getApplicationContext(),"SUCCESS",Toast.LENGTH_LONG).show();

                }
                else
                {
                    Toast.makeText(getApplicationContext(),"FAIL",Toast.LENGTH_LONG).show();
                }








                 Intent updateAct=new Intent(getApplicationContext(),update.class);
                 startActivity(updateAct);

            }

            });
















         deleteButton = (Button) findViewById(R.id.deleteBTN);
         deleteButton.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {

                Intent updateAct=new Intent(getApplicationContext(),update.class);
                 startActivity(updateAct);

            }

            });

         summaryButton = (Button) findViewById(R.id.summaryBTN);
         summaryButton.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {

                Intent summaryView=new Intent(getApplicationContext(),summary.class);
                 startActivity(summaryView);

            }

            });



        }
    public static class DatePickerFragment extends DialogFragment
    implements DatePickerDialog.OnDateSetListener {

        public EditText editText;
        DatePicker dpResult;

    public Dialog onCreateDialog(Bundle savedInstanceState) {
    // 

使用当前日期作为选择器中的默认日期

final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
//return new DatePickerDialog(getActivity(), (EditSessionActivity)getActivity(), year, month, day);

// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}

public void onDateSet(DatePicker view, int year, int month, int day) {

    updateEdit.setText(String.valueOf(day) + "/"
            + String.valueOf(month + 1) + "/" + String.valueOf(year));
    // set selected date into datepicker also


}
}

}

selection.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
     android:orientation="vertical"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >


   <TableLayout 
        android:id="@+id/tableLayout1" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:stretchColumns="1 ">

        <TableRow 
            android:id="@+id/tableRow1" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content">

             <Button
            android:id="@+id/newBTN"
            android:text="New"
            android:layout_width="wrap_content"
            android:layout_height="60px" >
        </Button>
       </TableRow>

         <TableRow 
            android:id="@+id/tableRow2" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
            <Button
            android:id="@+id/updateBTN"
            android:text="Update"
            android:layout_width="wrap_content"
            android:layout_height="60px" >
        </Button>
            <EditText
                android:id="@+id/updateEdit" 
                android:text="" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                  android:editable="false"
                android:enabled="true">
            </EditText>
        </TableRow>
         <TableRow 
            android:id="@+id/tableRow3" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
            <Button
            android:id="@+id/deleteBTN"
            android:text="Delete"
            android:layout_width="wrap_content"
            android:layout_height="60px" >
        </Button>
            <EditText
                android:id="@+id/deleteEdit" 
                android:text="" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
            </EditText>
        </TableRow>


          <TableRow 
            android:id="@+id/tableRow4" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
            <Button
            android:id="@+id/summaryBTN"
            android:text="Summary"
            android:layout_width="wrap_content"
            android:layout_height="60px" >
        </Button>

        </TableRow>




        </TableLayout>





</LinearLayout>

1 个答案:

答案 0 :(得分:0)

根据您的问题,我了解您需要检索存储为数据库中的字符串的日期值并比较日期值。为此,您可以尝试以下代码示例。

// String date_fromDB contains the string value retrieved from database.

SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
Date strDate = sdf.parse(date_fromDB); // Date value from database is obtained.

Calendar currentDate = Calendar.getInstance(); // Get current date

if (new Date().after(strDate))  // compare date values
{
    // Implement what you want to do with this code.
}