Android记事本NotePad应用功能拓展(二)
一、实现功能
1.NotaPad添加代办功能
2.点击待办事件添加计划Plan
3.点击记事本添加备忘录
二、项目代码分析
1.点击待办事件添加计划Plan
点击待办事件
String title = data.getExtras().getString("title", null);
String content = data.getExtras().getString("content", null);
String time = data.getExtras().getString("time", null);
Plan newPlan = new Plan(title, content, time);
com.example.atry.Alarm.CRUD op = new com.example.atry.Alarm.CRUD(context);
op.open();
op.addPlan(newPlan);
Log.d(TAG, "onActivityResult: "+ time);
op.close();
相关类分析
CRUD:对计划Plan的增删改查进行封装
EditAlarmActivity:对计划Plan细小操作的实现
PlanAdapter:连接Plan数据库的适配器接口
PlanDatabase:Plan数据库的具体实现
PlanAdapter
package com.example.atry.Alarm;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import com.example.atry.R;
import java.util.ArrayList;
import java.util.List;
public class PlanAdapter extends BaseAdapter implements Filterable {
private Context mContext;
private List<Plan> backList;//用来备份原始数据
private List<Plan> planList;//这个数据是会改变的,所以要有个变量来备份一下原始数据
PlanAdapter.MyFilter mFilter;
public PlanAdapter(Context mContext, List<Plan> planList) {
this.mContext = mContext;
this.planList = planList;
backList = planList;
}
@Override
public int getCount() {
return planList.size();
}
@Override
public Object getItem(int position) {
return planList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
mContext.setTheme((sharedPreferences.getBoolean("nightMode", false)? R.style.NightTheme: R.style.DayTheme));
View v = View.inflate(mContext, R.layout.plan_layout, null);
TextView tv_title = (TextView)v.findViewById(R.id.tv_title);
TextView tv_content = (TextView)v.findViewById(R.id.tv_content);
TextView tv_time = (TextView)v.findViewById(R.id.tv_time);
//Set text for TextView
tv_title.setText(planList.get(position).getTitle());
tv_content.setText(planList.get(position).getContent());
tv_time.setText(planList.get(position).getTime());
//Save plan id to tag
v.setTag(planList.get(position).getId());
return v;
}
@Override
public Filter getFilter() {
if (mFilter ==null){
mFilter = new PlanAdapter.MyFilter();
}
return mFilter;
}
class MyFilter extends Filter {
//我们在performFiltering(CharSequence charSequence)这个方法中定义过滤规则
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
FilterResults result = new FilterResults();
List<Plan> list;
if (TextUtils.isEmpty(charSequence)) {//当过滤的关键字为空的时候,我们则显示所有的数据
list = backList;
} else {//否则把符合条件的数据对象添加到集合中
list = new ArrayList<>();
for (Plan plan : backList) {
if (plan.getTitle().contains(charSequence) || plan.getContent().contains(charSequence)) {
list.add(plan);
}
}
}
result.values = list; //将得到的集合保存到FilterResults的value变量中
result.count = list.size();//将集合的大小保存到FilterResults的count变量中
return result;
}
//在publishResults方法中告诉适配器更新界面
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
planList = (List<Plan>)filterResults.values;
if (filterResults.count>0){
notifyDataSetChanged();//通知数据发生了改变
}else {
notifyDataSetInvalidated();//通知数据失效
}
}
}
}
2.点击记事本事件添加笔记Note
点击记事本事件
String content = data.getExtras().getString("content");
String time = data.getExtras().getString("time");
int tag = data.getExtras().getInt("tag", 1);
Note newNote = new Note(content, time, tag);
CRUD op = new CRUD(context);
op.open();
op.addNote(newNote);
op.close();
achievement.addNote(content);
相关类分析
CRUD:对计划Note的增删改查进行封装
EditActivity:对计划Note细小操作的实现
NoteAdapter:连接Note数据库的适配器接口
NoteDatabase:Note数据库的具体实现
NoteAdapter
package com.example.atry;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class NoteAdapter extends BaseAdapter implements Filterable {
private Context mContext;
private List<Note> backList;//用来备份原始数据
private List<Note> noteList;//这个数据是会改变的,所以要有个变量来备份一下原始数据
private MyFilter mFilter;
public NoteAdapter(Context mContext, List<Note> noteList) {
this.mContext = mContext;
this.noteList = noteList;
backList = noteList;
}
@Override
public int getCount() {
return noteList.size();
}
@Override
public Object getItem(int position) {
return noteList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
mContext.setTheme((sharedPreferences.getBoolean("nightMode", false)? R.style.NightTheme: R.style.DayTheme));
View v = View.inflate(mContext, R.layout.note_layout, null);
TextView tv_content = (TextView)v.findViewById(R.id.tv_content);
TextView tv_time = (TextView)v.findViewById(R.id.tv_time);
//Set text for TextView
String allText = noteList.get(position).getContent();
if (sharedPreferences.getBoolean("noteTitle" ,true))
tv_content.setText(allText.split("\n")[0]);
else tv_content.setText(allText);
tv_time.setText(noteList.get(position).getTime());
//Save note id to tag
v.setTag(noteList.get(position).getId());
return v;
}
@Override
public Filter getFilter() {
if (mFilter ==null){
mFilter = new MyFilter();
}
return mFilter;
}
class MyFilter extends Filter {
//我们在performFiltering(CharSequence charSequence)这个方法中定义过滤规则
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
FilterResults result = new FilterResults();
List<Note> list;
if (TextUtils.isEmpty(charSequence)) {//当过滤的关键字为空的时候,我们则显示所有的数据
list = backList;
} else {//否则把符合条件的数据对象添加到集合中
list = new ArrayList<>();
for (Note note : backList) {
if (note.getContent().contains(charSequence)) {
list.add(note);
}
}
}
result.values = list; //将得到的集合保存到FilterResults的value变量中
result.count = list.size();//将集合的大小保存到FilterResults的count变量中
return result;
}
//在publishResults方法中告诉适配器更新界面
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
noteList = (List<Note>)filterResults.values;
if (filterResults.count>0){
notifyDataSetChanged();//通知数据发生了改变
}else {
notifyDataSetInvalidated();//通知数据失效
}
}
}
}
四、参考资料
相关参考:Android记事本NotePad应用功能拓展(一)
作者GitHub下载链接:https://github.com/KangKangBigWorld/Android/tree/master/Mid_Test