zoukankan      html  css  js  c++  java
  • 记事本3

    记事本3

    fangyukuan

    2010-4-9

     

    完善第二个例子。学习目标:

    l事件生命周期

    l维持应用程序状态

     

    1)NoteEdit

    在onCreate删除以下代码。即我们不再需要从意图取得标题和内容,我们只需要从意图取得行ID就行了。然后用行的ID去数据取,标题和内容。

    String title = extras.getString(NotesDbAdapter.KEY_TITLE);

    String body = extras.getString(NotesDbAdapter.KEY_BODY);

     

    if (title != null)

    {

        mTitleText.setText(title);

    }

    if (body != null)

    {

        mBodyText.setText(body);

    }

     

    NoteEdit 类加数据成员

    private NotesDbAdapter mDbHelper;

     

    onCreatesuper.onCreate()后面加以下代码,因为我们要用到数据库。

    mDbHelper = new NotesDbAdapter(this);

    mDbHelper.open();

     

    confirmButton.setOnClickListener()之前调用()方法。

     

    NoteEdit 类加方法populateFields

        // 根据行号从数据库取标题和内容

        private void populateFields()

        {

            if (null != mRowId)

            {

                Cursor note = mDbHelper.fetchNote(mRowId);

                startManagingCursor(note);

                mTitleText.setText(note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));

                mBodyText.setText(note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));

            }

        }

     

    因为我们在点击确定的时候,不再需要返回结果,所以我们把onClick的代码做相应修改:

     

                public void onClick(View view)

                {

                    Bundle bundle = new Bundle();

     

                    bundle.putString(NotesDbAdapter.KEY_TITLE, mTitleText.getText()

                            .toString());

                    bundle.putString(NotesDbAdapter.KEY_BODY, mBodyText.getText()

                            .toString());

                    if (mRowId != null)

                    {

                        bundle.putLong(NotesDbAdapter.KEY_ROWID, mRowId);

                    }

     

                    Intent mIntent = new Intent();

                    mIntent.putExtras(bundle);

                    setResult(RESULT_OK, mIntent);

                    finish();

                }

     

    改成:

                public void onClick(View view)

                {

                    setResult(RESULT_OK);

                    finish();

                }

     

     

    重写三个方法onSaveInstanceState(), onPause() and onResume()

        // Activity在恢复之前被杀掉时,Android会调用这个方法。

        // 所以我们可以在这里保存一些信息,以使下次重新启动时恢复到现在的状态

        // 参数outState onCreate的参数savedInstanceState是一样的

        @Override

        protected void onSaveInstanceState(Bundle outState)

        {

            super.onSaveInstanceState(outState);

            outState.putLong(NotesDbAdapter.KEY_ROWID, mRowId);

        }

     

        // Activity结束时onPause会被调用(比如说finish方法调用时)

        // 很多时候我们可以在这里释放资源

        // 我们在这里把数据保存到数据库里

        @Override

        protected void onPause()

        {

            super.onPause();

            saveState();

        }

     

        // 这个就是Activity恢复的时候会被调用

        @Override

        protected void onResume()

        {

            super.onResume();

            populateFields();

        }

     

     

    2)Notepadv3

    因为记事本的存取我们都在NoteEdit 里做了。所以在onActivityResult里我们只需要更新一下视图的数据。而不用再做其它的事。所以把数据库相关的操作删除掉。

        @Override

        protected void onActivityResult(int requestCode, int resultCode,

                Intent intent)

        {

            super.onActivityResult(requestCode, resultCode, intent);

            fillData();

        }

     

    删除方法onListItemClick以下代码。当点击某一列时,我们只需要把行ID,放进意图里。不再需要把标题和内容也放进去。因为用行ID可以在数据查找到标题和内容。

    Cursor c = mNotesCursor;

    c.moveToPosition(position);

     

    i.putExtra(NotesDbAdapter.KEY_TITLE, c.getString(c

                    .getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));

    i.putExtra(NotesDbAdapter.KEY_BODY, c.getString(c

                    .getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));

    修改之后:

        @Override

        protected void onListItemClick(ListView l, View v, int position, long id)

        {

            super.onListItemClick(l, v, position, id);

            Intent i = new Intent(this, NoteEdit.class);

            i.putExtra(NotesDbAdapter.KEY_ROWID, id);

            startActivityForResult(i, ACTIVITY_EDIT);

        }

     

    修改方法fillData以下代码:

    mNotesCursor = mDbHelper.fetchAllNotes();

     

    mNotesCursor改成局部变量。当然相应的mNotesCursor也得改成局部变量notesCursor(记得把mNotesCursor定义也删除掉)

    Cursor notesCursor = mDbHelper.fetchAllNotes();

     http://www.cnblogs.com/fangyukuan/archive/2010/04/09/1704849.html



     

  • 相关阅读:
    vscode英文显示设置为中文语言
    vscode各插件使用-背景图-scss
    公众号关联小程序
    js点击遮罩空白区域关闭,点击遮罩内元素不关闭
    swiper使用-点击跳转指定页
    小程序内部引导关注公众号实现方法
    jq赋值input值为空
    sass中代码使用
    Sources”参数中指定了多次。“Sources”参数不支持重复项
    将已存在小程序项目添加云开发配置
  • 原文地址:https://www.cnblogs.com/fangyukuan/p/1704849.html
Copyright © 2011-2022 走看看