0
我會使用日期選擇器將DATE插入到DATABASE中。它將作爲STRING存儲在數據庫中。我的另一個步驟是檢查日期。通過這個,我的意思是我會有一個edittext和一個按鈕。在編輯文本中,它將使用datapicker選擇日期。如何確保所選日期在數據庫中相同?
點擊按鈕,它會檢查數據庫是否有日期。如果有,它會直接到另一個頁面,否則它不會提供這樣的日期。
這是我的代碼:
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);
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)
{
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) {
// Use the current date as the default date in the picker
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
}
}
}
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
}
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>
從您的數據庫中獲取所有日期,並與您在循環中選擇的日期進行比較。插入的日期與您從日期選擇器中選擇的日期格式相同嗎? – longwalker
可能重複的[日期比較在Android](http://stackoverflow.com/questions/10008911/date-comparison-in-android) –
@longwalker插入字符串中的日期.. – Chloe