2017-06-27 11 views
0

分と曜日と時間のある日付を保存する方法選択した日付と選択した時間をデータベースに挿入します。androidのsqliteデータベースにdatepickerとtimepickerを挿入するには

public class FutureSms extends AppCompatActivity { 
DbHelper db; 
EditText name,mob,msg; 
DatePicker dp; 
TimePicker tp; 
Button b1; 
Button b2; 


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

    db = new DbHelper(this); 
    name = (EditText) findViewById(R.id.name); 
    dp = (DatePicker) findViewById(R.id.date); 
    tp = (TimePicker) findViewById(R.id.time); 
    mob = (EditText) findViewById(R.id.mobnumber); 
    msg = (EditText) findViewById(R.id.msg); 
    b1 = (Button) findViewById(R.id.save); 
    b2 = (Button) findViewById(R.id.view); 
    AddData(); 
} 
public void AddData() 
{ 
    b1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 



      boolean isInserted = db.insertData(
        name.getText().toString(), 
        dp.getDayOfMonth(); 
        tp.getCurrentHour(); 
        mob.getText().toString(), 
        msg.getText().toString()); 
      if(isInserted == true) 
       Toast.makeText(FutureSms.this,"Data Inserted",Toast.LENGTH_LONG).show(); 
      else 
       Toast.makeText(FutureSms.this,"Data not Inserted",Toast.LENGTH_LONG).show(); 
     } 
    }); 
}} 

これはビューのレイアウトのxmlです。

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Select a time to send Message" 
    android:textColor="#3B5998" 
    android:textSize="20dp" 
    android:textStyle="bold" 
    android:layout_gravity="center_horizontal" /> 

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:layout_marginTop="30dp"> 

     <DatePicker 
      android:id="@+id/pickerdate" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
     <TimePicker 
      android:id="@+id/pickertime" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
     <Button 
      android:id="@+id/setalarm" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Set Alarm"/> 
     <TextView 
      android:id="@+id/info" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"/> 

    </LinearLayout> 
</ScrollView> 

DbHealper(このコードは、挿入、削除、updationおよびビューのストアデータベースに含まれている)は、Javaで

public class DbHelper extends SQLiteOpenHelper { 

public static final String DATABASE_NAME = "Alert.db"; 
public static final String TABLE_NAME = "MessageAlert"; 
public static final String COL = "ID" ; 
public static final String COL_1 = "NAME"; 
public static final String COL_2 = "DATE"; 
public static final String COL_3 = "TIME"; 
public static final String COL_4= "MOBILE"; 
public static final String COL_5 = "MESSAGE"; 

public DbHelper(Context context) { 
    super(context, DATABASE_NAME , null, 1); 
} 

@Override 
public void onCreate(SQLiteDatabase sqLiteDatabase) { 

    sqLiteDatabase.execSQL("create table" + TABLE_NAME + "(ID INTEGER PRIMERY KEY AUTOINCREMENT ,NAME TEXT,DATE TEXT,TIME TEXT,MOBILE TEXT,MESSAGE TEXT } "); 

} 

@Override 
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { 

    sqLiteDatabase.execSQL("IF DROP TABLE EXISTS" + TABLE_NAME); 
    onCreate(sqLiteDatabase); 

} 

public boolean insertData(String name,String date,String time,String mobile,String message) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put(COL_1,name); 
    contentValues.put(COL_2,date); 
    contentValues.put(COL_3,time); 
    contentValues.put(COL_4,mobile); 
    contentValues.put(COL_5,message); 
    long result = db.insert(TABLE_NAME,null ,contentValues); 
    if(result == -1) 
     return false; 
    else 
     return true; 
} 

public Integer deleteData (String id) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    return db.delete(TABLE_NAME, "ID = ?",new String[] {id}); 
} 



public Cursor getAllData() { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor res = db.rawQuery("select * from "+TABLE_NAME,null); 
    return res; 
} 

public boolean updateData(String id,String name,String date,String time,String mobile,String message) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put(COL,id); 
    contentValues.put(COL_1,name); 
    contentValues.put(COL_2,date); 
    contentValues.put(COL_3,time); 
    contentValues.put(COL_4,mobile); 
    contentValues.put(COL_5,message); 
    db.update(TABLE_NAME, contentValues, "ID = ?",new String[] { id }); 
    return true; 
}} 
+0

だから問題は何ですか?あなたには例外がありますか? – Yupi

答えて

1

Dateは1970年1月1日00からのミリ秒だけの量である:00:00.000 GMT 。だからあなたは前後に長い変数にそれを変換することができます。 Date.getTime()longの日付の値を取得する(UNIXのタイムスタンプはミリ秒単位)。 new Date(System.currentTimeMillis())を使用してDateオブジェクトを作成します。したがって、最終的には、データベースに1つの長い値を格納するだけで済みます。

関連する問題