gpt4 book ai didi

java - 如何在android中的sqlite数据库中插入日期选择器和时间选择器

转载 作者:行者123 更新时间:2023-12-02 12:45:57 25 4
gpt4 key购买 nike

如何存储带日期的日期和带分钟的时间,我想在数据库中插入选定的日期和选定的时间。

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();
}
});
}}

这是 View 布局的 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(此代码包含存储数据库以进行插入、删除、更新和查看)

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;
}}

最佳答案

Java 中的

Date 只是自 1970 年 1 月 1 日 00:00:00.000 GMT 以来的毫秒数。所以你可以将它来回转换为长变量。Date.getTime() 获取日期的 long 值(以毫秒为单位的 UNIX 时间戳)。并使用 new Date(System.currentTimeMillis()) 来创建 Date 对象。因此最终您只需在数据库中存储一个长值即可。

关于java - 如何在android中的sqlite数据库中插入日期选择器和时间选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44783257/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com