今天写了数据库的相关操作,可实现新笔记的添加
遇到的问题:写了添加之后刷新页面的函数,运行时会闪退,已解决
明天实现笔记显示界面的美化
NoteDatabase.java:
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class NoteDatabase extends SQLiteOpenHelper { public static final String TABLE_NAME = "notes"; public static final String CONTENT = "content"; public static final String ID = "_id"; public static final String TIME = "time"; public static final String MODE = "mode"; public NoteDatabase(Context context){ super(context, "notes", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE "+ TABLE_NAME + "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + CONTENT + " TEXT NOT NULL," + TIME + " TEXT NOT NULL," + MODE + " INTEGER DEFAULT 1)" ); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { /*for(int i = oldVersion; i < newVersion; i++) { switch (i) { case 1: break; case 2: updateMode(db); default: break; } }*/ } }
NoteAdapter.java:
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 context; private List<Note> backList; //备份原始数据 private List<Note> noteList; private MyFilter mFilter; public NoteAdapter(Context mContext,List<Note> noteList){ this.context = 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(context); context.setTheme(R.style.DayTheme); View v = View.inflate(context,R.layout.note_layout,null); TextView content = (TextView)v.findViewById(R.id.tv_content); TextView 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(" ")[0]);*/ content.setText(allText); 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; } @Override protected void publishResults(CharSequence charSequence, FilterResults filterResults) { noteList = (List<Note>)filterResults.values; if (filterResults.count > 0){ notifyDataSetChanged();//通知数据发生了改变 }else { notifyDataSetInvalidated();//通知数据失效 } } } }