任务6.1:实现笔记本(3)

2018-05-08  本文已影响0人  jingz课程

上一篇教程的最后,我们实现了新建笔记本以及笔记本的切换。然后新的笔记本中并没有关联任何一条笔记。我们需要修改新建笔记页面,为其提供设置新笔记所属笔记本的入口。
修改新建页面为如下效果:

与原来相比,在标题编辑框下多了一个当前笔记本视图。点击视图,即弹出NotebooksActivity,由用户选择笔记本。如果没有合适的笔记本,还可以就地新建后再选择。

修改EditNoteActivity类的布局文件,在紧邻标题编辑框下方添加笔记本视图:

<?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:orientation="vertical"
    tools:context="com.jing.app.sn.EditNoteActivity">

    <EditText
        android:id="@+id/edit_title"
        ...

    <!--添加笔记本视图-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/edit_padding_horizontal"
        android:paddingRight="@dimen/edit_padding_horizontal"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:clickable="true"
        android:onClick="onSelectNotebook"
        android:background="@color/white">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_sel_nb"/>

        <TextView
            android:id="@+id/tv_notebook"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:textColor="#666666"
            android:textSize="14sp"
            android:text="@string/all_notes"/>

    </LinearLayout>
    ...
</LinearLayout>

所需要得图标如下:

ic_sel_nb.png

我们添加的是一个水平方向的线性布局,内部包含一个笔记本图标和一个文本视图tv_notebook。当我们选择了新的笔记本并返回时,应当更新tv_notebook的内容。

打开EditNoteActivity类,添加如下的成员变量:

    // 新笔记所属的笔记本,缺省为全部笔记
    private Notebook mNotebook;
    // 笔记本名字视图
    private TextView mNotebookView;

找到onCreate()方法,在它的最后添加初始化代码:

        mNotebook = new Notebook(0, getString(R.string.all_notes));
        mNotebookView = findViewById(R.id.tv_notebook);
        mNotebookView.setText(mNotebook.getName());

然后创建点击事件处理函数onSelectNotebook(),并仿造之前的方式调起NotebooksActivity:

    public void onSelectNotebook(View view) {
        Intent intent = new Intent(this, NotebooksActivity.class);
        startActivityForResult(intent, 1);
    }

接下来,仍然重写onActivityResult()方法来处理返回的笔记本数据:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1 && resultCode == 1) {
            // 重新设置notebookId成员
            long notebookId = data.getLongExtra("notebookId", 0);
            String notebookName = data.getStringExtra("notebookName");

            // 重设当前笔记本
            mNotebook.setId(notebookId);
            mNotebook.setName(notebookName);
            // 刷新当前笔记本名字
            mNotebookView.setText(mNotebook.getName());
        }
    }

最后,当保存新笔记时,要将选择的笔记本id一并保存。找到处理数据保存的onFinishEdit()方法。我们原来在这个方法内部,创建了一个匿名的AsyncTask异步任务来完成数据库写入。对其中的doInBackground()方法进行修改,用mNotebook成员记录的笔记本id来设置被保存的笔记对象:

            @Override
            protected Note doInBackground(Void... voids) {
                // 1. 从编辑区获取标题和内容字符串
                String title = mTitleEdit.getEditableText().toString();
                String content = mContentEdit.getEditableText().toString();

                // 2. 创建笔记对象
                Note note = new Note(0, title, content, System.currentTimeMillis(), mNotebook.getId());

                return noteRepository.saveNote(note);
            }

完成后运行程序,即可创建专属某一笔记本的笔记了:

上一篇 下一篇

猜你喜欢

热点阅读