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
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);
itemNoteDate = itemView.findViewById(R.id.item_note_date);
}
时间布局
<TextView
android:id="@+id/item_note_date"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="16sp"
android:gravity="center_vertical"
android:singleLine="true"/>
===============================================================
通过标题来模糊查询数据库的数据
private void getSelectNoteList(String title){
noteList.clear();
Cursor allNotes = Note.selectNotes(dbHelper,title);
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);
}
}
当EditText内容发生变化时会调用此方法,根据内容变化动态来查询数据库的数据,然后把数据装配到适配器中,从而把列表显示出来
et_title.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
//当EditText内容发生变化时会调用此方法
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.toString().equals("")){
search_del.setVisibility(View.GONE);
}else{
search_del.setVisibility(View.VISIBLE);
}
String titleChange=et_title.getText().toString();
Log.v("改变数据:",titleChange);
getSelectNoteList(titleChange);
mListAdapter.notifyDataSetChanged();
}
@Override
public void afterTextChanged(Editable editable) {
}
});
这个方法没有第一种方法人性化,用户体验较差
给查询按钮添加一个监听器
private void setListener(){
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,EditActivity.class);
startActivity(intent);
}
});
selectBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String edit_title=et_title.getText().toString();
HashMap<Integer,HashMap<String,Object>> map1=new HashMap<Integer,HashMap<String,Object>>();;
map1=Note.getNote(dbHelper,edit_title);
Log.v("查询",map1.toString());
Intent intent = new Intent(MainActivity.this,SelectActivity.class);
intent.putExtra("edit_title",edit_title );
startActivity(intent);
//getSelectNoteList(edit_title);
}
});
把EditTtext的内容置为空
search_del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getNoteList();;//初始化数据
et_title.setText("");
mListAdapter.notifyDataSetChanged();//更新RecycleView
}
});
========================================================================
插入图片有多种方式,这里简单介绍两种,分别是从本地获取图片以及拍照直接插入图片。
在AndroidManifest.xml中设置权限
如果是Android7.0以上的版本需要在onCreate()这个方法中加入以下代码,不然启动拍照功能的时候会出现路径不对的问题:
// android 7.0系统解决拍照的问题
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
builder.detectFileUriExposure();
通过设置intent的类型来启动图片相关的intent
//从相册取图片
public void getPhoto() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
startActivityForResult(intent, PHOTO_FROM_GALLERY);
}
通过使用正则表达式来提取图片路径
private static final String regex="content://com.android.providers.media.documents/"
+"document/image%\w{4}";
private static final String reg="file:///storage/emulated/0/\d+.jpg";
获取权限并且启动拍照相关的intent
//拍照取照片
public void takeCamera() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions( this, new String[] { Manifest.permission.CAMERA }, PHOTO_FROM_CAMERA);
}
else {
File file=new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg");
try {
if(file.exists()){
file.delete();
}
file.createNewFile();
}catch (IOException e){
e.printStackTrace();
}
imageUri=Uri.fromFile(file);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
startActivityForResult(intent, PHOTO_FROM_CAMERA);
}
}
把图片转换形式并加入文本框,执行保存时把图片存进数据库
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
ContentResolver resolver = getContentResolver();
super.onActivityResult(requestCode, resultCode, data);
//第一层switch
switch (requestCode) {
case PHOTO_FROM_GALLERY:
//第二层switch
switch (resultCode) {
case RESULT_OK:
if (data != null) {
Uri uri = data.getData();//获取Intent uri
Bitmap bitmap = null;
//判断该路径是否存在
try {
Bitmap originalBitmap = BitmapFactory.decodeStream(resolver.openInputStream(uri));
bitmap = resizeImage(originalBitmap, 200, 200);//图片缩放
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if(bitmap != null){//如果图片存在
//将选择的图片追加到EditText中光标所在位置
int index = mText.getSelectionStart(); //获取光标所在位置
Editable edit_text = mText.getEditableText();//编辑文本框
if(index <0 || index >= edit_text.length()){
edit_text.append(uri.toString());
updateNoteText(mText.getText().toString());//更新到数据库
}else{
edit_text.insert(index,uri.toString());
updateNoteText(mText.getText().toString());//更新到数据库
}
}else{
最后
Android学习是一条漫长的道路,我们要学习的东西不仅仅只有表面的 技术,还要深入底层,弄明白下面的 原理,只有这样,我们才能够提高自己的竞争力,在当今这个竞争激烈的世界里立足。
人生不可能一帆风顺,有高峰自然有低谷,要相信,那些打不倒我们的,终将使我们更强大,要做自己的摆渡人。
我把自己这段时间整理的Android最重要最热门的学习方向资料放在了我的GitHub,里面还有不同方向的自学编程路线、面试题集合/面经、及系列技术文章等。
资源持续更新中,欢迎大家一起学习和探讨。