Android 记事本功能的实现
时间戳
时间戳的编写包括记事本编辑区域的时间展示和记事本列表的时间展示
编辑框的时间戳
获取系统时间并转换格式
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Log.v("时间:",sdf.format(date));
tv_now.setText(sdf.format(date));
把时间保存到数据库,每次修改都更新一次时间
private void saveNote(){
NoteDataBaseHelper dbHelper = MainActivity.getDbHelper();
ContentValues values = new ContentValues();
values.put(Note.title,et_title.getText().toString());
values.put(Note.content,et_content.getText().toString());
values.put(Note.time,tv_now.getText().toString());
if (insertFlag){
Note.insertNote(dbHelper,values);
}else{
Note.updateNote(dbHelper,Integer.parseInt(currentNote.getId()),values);
}
}
关于时间的布局
<TextView
android:id="@+id/tv_now"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#A5BECE"
android:textAlignment="textEnd"
android:gravity="end|center_vertical"
android:paddingRight="8dp"/>
初始化视图
private void initView(){
btn_save = findViewById(R.id.btn_save);
btn_return = findViewById(R.id.btn_return);
tv_now = findViewById(R.id.tv_now);
et_content = findViewById(R.id.edit_content);
et_title = findViewById(R.id.edit_title);
//以下为测试代码
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Log.v("时间:",sdf.format(date));
tv_now.setText(sdf.format(date));
}
记事本列表的时间戳
从数据库获取每个笔记的时间
//从数据库中读取所有笔记 封装成List<NoteInfo>
private void getNoteList(){
noteList.clear();
Cursor allNotes = Note.getAllNotes(dbHelper);
for (allNotes.moveToFirst(); !allNotes.isAfterLast(); allNotes.moveToNext()){
NoteInfo noteInfo = new NoteInfo();
noteInfo.setId(allNotes.getString(allNotes.getColumnIndex(Note._id)));
noteInfo.setTitle(allNotes.getString(allNotes.getColumnIndex(Note.title)));
noteInfo.setContent(allNotes.getString(allNotes.getColumnIndex(Note.content)));
noteInfo.setDate(allNotes.getString(allNotes.getColumnIndex(Note.time)));
noteList.add(noteInfo);
}
}
初始化试图
public ViewHolder(View itemView) {
if (itemView == null){
throw new IllegalArgumentException("item View can not be null!");
}
this.itemView = itemView;
itemIcon = itemView.findViewById(R.id.rand_icon);
itemNoteTitle = itemView.findViewById(R.id.item_note_title);
itemNo