1.项目的创建
打开Android studio新建一个MyNotePad项目.
2.导入依赖以及相关配置
app 的build.gradle文件头部添加
apply plugin: 'android-apt'apply plugin: 'org.greenrobot.greendao'
下面是添加的依赖项目
compile 'com.android.support:cardview-v7:25.3.1'compile 'com.jakewharton:butterknife:8.6.0'apt 'com.jakewharton:butterknife-compiler:8.6.0'compile 'com.android.support:recyclerview-v7:25.3.1'compile'org.greenrobot:greendao:3.2.2'compile'org.greenrobot:greendao-generator:3.2.2'
工程的builde.gradle文件
buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.3.3' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } }
app 的build.gradle文件android目录添加
greendao { schemaVersion 1 daoPackage 'nanjing.jun.mynotepad.entity' targetGenDir 'src/main/java'}
3.创建一个日记的实体类
我们这里只要把这个实体类的属性和get,setter写好就行了
@Entitypublic class Note { private Long id; private String content; private String date; public Note(Long id, String content, String date) { this.id = id; this.content = content; this.date = date; } public Note() { } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getContent() { return this.content; } public void setContent(String content) { this.content = content; } public String getDate() { return this.date; } public void setDate(String date) { this.date = date; } }
实体类写好了之后我们build一次工程,我们会发现我们的实体类被改造了
@Entitypublic class Note { @Id private Long id; @Property(nameInDb = "NOTECONTENT") private String content; @Property(nameInDb = "NOTEDATE") private String date; @Generated(hash = 1881903072) public Note(Long id, String content, String date) { this.id = id; this.content = content; this.date = date; } @Generated(hash = 1272611929) public Note() { } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getContent() { return this.content; } public void setContent(String content) { this.content = content; } public String getDate() { return this.date; } public void setDate(String date) { this.date = date; } }
我们这个应用就创建这一个实体类就行了.在我们创建好了这个实体类后,我们build一次工程,让greendao-generator帮我们自动生成相应的代码.
如下截图,是build之后生成的类
我们看到自动帮我们生成了DaoMaster,DaoSession,NoteDao文件
4.接下来我们重写一个Application
public class NoteApp extends Application { private static NoteApp noteApp; private DaoMaster.DevOpenHelper helper; private SQLiteDatabase database; private DaoMaster daoMaster; private DaoSession daoSession; public static NoteApp getInstnca() { return noteApp; } @Override public void onCreate() { super.onCreate(); noteApp = this; setDatabase(); } private void setDatabase() { helper = new DaoMaster.DevOpenHelper(this, "notedb", null); database = helper.getWritableDatabase(); daoMaster = new DaoMaster(database); daoSession = daoMaster.newSession(); } public DaoSession getDaoSession() { return daoSession; } public SQLiteDatabase getDatabase() { return database; } }
我们主要看下setDataBase方法就行了,我们在这里主要拿到这个session就行了,就让这个session来干活,最后别忘了在manifest文件里面注册一下.
5.完成新增日记的界面
看下新增日记界面的布局文件activity_create_note.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f0f0f0" android:orientation="vertical" tools:context="nanjing.jun.mynotepad.MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <EditText android:id="@+id/note_content" android:layout_width="match_parent" android:layout_height="200dp" android:layout_margin="10dp" android:background="#fff" android:gravity="start" android:hint="记录每天点点滴滴" android:padding="5dp" /> <Button android:id="@+id/save" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="保存" /></LinearLayout>
就一个EditText和一个Button,接下就是实现对应的activity了
public class CreateNoteActivity extends AppCompatActivity { @BindView(R.id.toolbar) Toolbar toolbar; @BindView(R.id.note_content) EditText noteContent; @BindView(R.id.save) Button save; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_create_note); ButterKnife.bind(this); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } @OnClick(R.id.save) public void onClick() { if (noteContent.getText().toString().length() == 0) { Toast.makeText(this, "未输入任何内容", Toast.LENGTH_SHORT).show(); return; } Note note = new Note(); note.setContent(noteContent.getText().toString()); note.setDate(simpleDateFormat.format(new Date())); NoteApp.getInstnca().getDaoSession().getNoteDao().insert(note); finish(); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); break; } return super.onOptionsItemSelected(item); } }
CreateNoteActivity最关键的一句代码就是NoteApp.getInstnca().getDaoSession().getNoteDao().insert(note);插入一条记录.我们可以重写一下Note实体类的toString方法然后在insert之后添加一条打印语句
Log.i(“插入的数据”, NoteApp.getInstnca().getDaoSession().getNoteDao().loadAll().toString());我们看下运行的结果
这里我添加了3个日记,确实都添加成功了.
6.我们实现主页面的列表
布局文件我就不放了,就是一个Recyclerview然后右下角有一个FloatingActionButton用来启动创建日记的按钮,看下Activity 的代码
public class MainActivity extends AppCompatActivity { @BindView(R.id.toolbar) Toolbar toolbar; @BindView(R.id.notes) RecyclerView notes; @BindView(R.id.fab) FloatingActionButton fab; NoteAdapter noteAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); setSupportActionBar(toolbar); notes.setLayoutManager(new LinearLayoutManager(this)); noteAdapter = new NoteAdapter(); notes.setAdapter(noteAdapter); } @Override protected void onResume() { super.onResume(); List<Note> notes = NoteApp.getInstnca().getDaoSession().getNoteDao().loadAll(); noteAdapter.setNotes(notes); } @OnClick(R.id.fab) public void onClick() { Intent intent = new Intent(this, CreateNoteActivity.class); startActivity(intent); } }
MainActivity最主要的代码是
List notes = NoteApp.getInstnca().getDaoSession().getNoteDao().loadAll();
到这里一个小小的记事本就完成了,是不是很简单?
看下最后App的主页面
7.接下来再实现一个删除的功能
我们在RecyclerView 的Adapter里面添加一个接口,
public interface OnNoteListener { void onNoteClick(Note note); void onNoteLongClick(Note note); }
这个很明显啦,就是单击的时候是对日记进行修改,长按的时候进行删除操作,我们看一下删除的回调方法
@Override public void onNoteLongClick(final Note note) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("提示信息"); builder.setTitle("确认删除吗?"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { NoteApp.getInstnca().getDaoSession().getNoteDao().delete(note); List<Note> notes = NoteApp.getInstnca().getDaoSession().getNoteDao().loadAll(); noteAdapter.setNotes(notes); } }); builder.setNegativeButton("算了", null); builder.create().show(); }
这个方法最主要的一句代码就是NoteApp.getInstnca().getDaoSession().getNoteDao().delete(note);通过指定的一个Note来删除数据,我们看下效果
我们看到这里确实删除成功了,好了至于修改的功能也难不倒哪去,照葫芦壶瓢呗,这里就不实现了.