一般在使用资料库新增资料的时候,都会看到新建资料的日期跟时间,今天会再sqllite上加入日期。
我是用新的资料库跟资料表来新增资料表栏位,其余栏位不变。
日期跟时间使用INTEGER类型建立
@Override
public void onCreate(SQLiteDatabase db) {
String SqlTable = "CREATE TABLE IF NOT EXISTS User (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"user text not null," +
"salary text not null," +
"currentTime INTEGER not null" +
")";
db.execSQL(SqlTable);
}
取得日期的方法
public String getTodayTime() {
//String dateformat = "yyyyMMdd"; 成果图第一个日期显示的格式
String dateformat = "yyyy/MM/dd"; //日期的格式(第二个)
Calendar mCal = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat(dateformat);
String today = df.format(mCal.getTime());
return today;
}
SimpleDateFormat处理Calendar跟String之间的转换
更改资料
public void UpdateData(View view) {
String user =name.getText().toString();
String userSalary =salary.getText().toString();
String dateTime =getTodayTime(); //取得日期
if (!user.isEmpty() ||!userSalary.isEmpty()) {
ContentValues contentValues = new ContentValues();
contentValues.put("user", user);
contentValues.put("salary", userSalary);
contentValues.put("currentTime",dateTime);
db.update(DataBaseTable, contentValues, "_id =" + 2,null );
Toast.makeText(this,"更新完成",Toast.LENGTH_SHORT).show();
dataList=showData();
}
}
新增资料
public void AddList(View view) {
String user =name.getText().toString();
String userSalary =salary.getText().toString();
String dateTime = getTodayTime(); //取得日期
if (!user.isEmpty() ||!userSalary.isEmpty()) {
ContentValues contentValues = new ContentValues();
contentValues.put("user", user);
contentValues.put("salary", userSalary);
contentValues.put("currentTime",dateTime);
db.insert(DataBaseTable, null, contentValues);
Toast.makeText(this,"新增完成",Toast.LENGTH_SHORT).show();
dataList=showData();
}
else{
Toast.makeText(this,"请填写资料",Toast.LENGTH_SHORT).show();
}
}
SimpleDateFormat日期时间的格式范例
最後在RecyclerviewAdapter绑定的layout那里新建一个TextVIew做显示,这样就完成了。
成果:
透过log看到新增上去的资料
在 Day03 我们使用 GCE 建立一台 VM,今天要学习如何连线到虚拟机,并在服务器上使用 No...
Obgect content Getter 与 Setter : 他们是 Object 内建的 p...
今天来聊聊系统开发前要做哪些事 还有接到需求後,要怎麽根据需求去设计系统架构 通常在开发系统前,都会...
来把卡摆上去吧 我们先来做卡的外型 放在 lib/card_web/component.ex 里面 ...
挑战最後一日的题目真的让我想了很久,倒底该放什麽元件来压轴才好?要写一个综合演练,把前面的元件都拿出...