4

当我单击它时,我有两个edittext,会弹出一个timepickerdialog,并且时间将在edittext中设置,因为我的代码是,

  int TIME_PICKER_ID = 1,hour,minute,flag;

  edittext_starttime.setOnClickListener(this);
  edittext_endtime.setOnClickListener(this);

    final Calendar c = Calendar.getInstance();
    hour = c.get(Calendar.HOUR_OF_DAY);
    minute = c.get(Calendar.MINUTE);

  @Override
   public void onClick(View v) {
     if(v.getId() == R.id.editText_starttime_stsa){
        flag = 1;
        showDialog(TIME_PICKER_ID);
    }//editText_starttime_stsa

    if(v.getId() == R.id.editText_endtime_stsa){
        flag = 2;
        showDialog(TIME_PICKER_ID);
    }//editText_endtime_stsa

    if(v.getId() == R.id.button_create_stsa){
        onCreateAlert();
    }//button_create_susa       
}

   @Override
protected Dialog onCreateDialog(int id) {
    switch(id){
    case 1: 
return new TimePickerDialog(this, timePickerListener, hour, minute, false);
    }
    return null;
}

private TimePickerDialog.OnTimeSetListener timePickerListener = new     TimePickerDialog.OnTimeSetListener(){
    public void onTimeSet(TimePicker view,int selectedHour,int selectedMinute){
        hour = selectedHour;
        minute = selectedMinute;

        updateDisplay(hour,minute);
    }
};

private void updateDisplay(int hour, int minute) {
    if(flag==1){
        edittext_starttime.setText(new StringBuilder().append(pad(hour)).append(":").append(pad(minute)));
    }else if(flag==2){
        edittext_endtime.setText(new StringBuilder().append(pad(hour)).append(":").append(pad(minute)));
    }
}

private static String pad(int c) {
    if (c >= 10)
       return String.valueOf(c);
    else
       return "0" + String.valueOf(c);
}

我想在我写过查询的 sqlite 数据库中插入 start_time 和 end_time,

String query = "insert into Status_Master(status_id, status_name, status_description,  timewindow_start, timewindow_end) values("+status_id+",'"+status_name+"','"+status_desc+"',time('"+start_time+"'),time('"+end_time+"'))";

但我收到错误。这是在数据库中插入时间的正确方法吗?

4

3 回答 3

2
  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 

  Date date1 = new Date();
  Date date2 = new Date();

    ContentValues initialValues = new ContentValues(); 
         initialValues.put("status_id",status_id);
         initialValues.put("status_name", status_name);
         initialValues.put("status_description", status_desc);



         initialValues.put("timewindow_start", dateFormat.format(date1));
          initialValues.put("timewindow_end", dateFormat.format(date2));

    long result= mDb.insert(DB_TABLE_NAME, null, initialValues);
于 2013-03-01T11:27:03.523 回答
1

这样做。首先创建数据库,我的代码在这里。

public class AppDatabase extends SQLiteOpenHelper
{
    private final static String DATABASE_NAME="AppDB";
    public final static String TABLE_PASSWORD="password";
    public static int version=1;
    public Context cont;
    public AppDatabase(Context context)
    {
        super(context,DATABASE_NAME, null, version);
        this.cont=context;
    }

    @Override
    public void onCreate(SQLiteDatabase sqlDb)
    {
        sqlDb.execSQL("Create table password(_id INTEGER autoincreament PRIMARY KEY, monthOfBirth INTEGER)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqlDb, int old, int latest)
    {
        sqlDb.execSQL("DROP TABLE password");
        this.onCreate(sqlDb);
    }
 }

现在创建一个事务类以通过方法将值插入数据库。

public long savePassword(int numberMonth)
    {
        long x=-1;
        sqliteDb=appDb.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("monthOfBirth", numberMonth);
        try
        {
            if(sqliteDb.isOpen())
            {
                x=sqliteDb.insert("password", null, values);
                if(x>=0)
                {
                    //Toast.makeText(context, "Password Save", Toast.LENGTH_SHORT).show();
                }
            }
        }
        catch(Exception exc)
        {
            System.err.print(exc);
        }
    return x;
    }

现在在您的活动中,只需从 edittext 中选择值,然后通过 Transaction 的对象调用此方法。

trans = new Transactions(MainActivity.this);
                            long x = trans.savePassword(pMonth);
                            if (x >= 0)
                                {
                                    Toast.makeText(getBaseContext(), "Password Save in Database.", Toast.LENGTH_SHORT).show();
                                } 
                            else
                                {
                                    Toast.makeText(getBaseContext(), "Password didn't save.",   Toast.LENGTH_SHORT).show();
                                }

希望这会有所帮助。

于 2013-03-01T11:22:01.747 回答
0

请检查SQLite 站点并查看时间函数。SQLite 仅支持站点中列出的 12 种格式。确保您提供的字符串格式正确。

于 2013-03-01T11:15:25.733 回答