Android Notepad Tutorial 学习笔记

  1. DbAdapter,作为content provider,通过继承SQLiteOpenHelper辅助实现
      private static class DatabaseHelper extends SQLiteOpenHelper {

    DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    db.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
    + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS notes");
    onCreate(db);
    }
    }
  1. SimpleCursorAdapter的用法

    Cursor NotesCursor = mDbHelper.fetchAllNotes();
    startManagingCursor(NotesCursor);

    // Create an array to specify the fields we want to display in the list (only TITLE)
    String[] from = new String[]{NotesDbAdapter.KEY_TITLE};

    // and an array of the fields we want to bind those fields to (in this case just text1)
    int[] to = new int[]{R.id.text1};

    // Now create a simple cursor adapter and set it to display
    SimpleCursorAdapter notes =
    new SimpleCursorAdapter(this, R.layout.notes_row, NotesCursor, from, to);
    setListAdapter(notes);
  2. 获取上下文菜单项

    public boolean onContextItemSelected(MenuItem item) {
    switch(item.getItemId()) {
    case DELETE_ID:
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    mDbHelper.deleteNote(info.id);
    fillData();
    return true;
    }
    return super.onContextItemSelected(item);
    }
  3. 通过Intent向下一个Activity传递参数

    Intent i = new Intent(this, NoteEdit.class);
    i.putExtra(NotesDbAdapter.KEY_ROWID, id);
    startActivityForResult(i, ACTIVITY_EDIT);
  4. onActivityResult的调用
    通过startActivityForResult打开的Activity,完成后返回时调用onActivityResult方法

  5. onSaveInstanceState的调用时点及用处

      protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putLong(NotesDbAdapter.KEY_ROWID, mRowId);
    }

onSaveInstanceState在程序退出或暂停时调用,通过Bundle保存的 值,在下次程序重新打开或恢复运行时会重新载入,这样我们就能在程序 下一次打开时还原用户的最后状态。

7.layout中的android:layout_weight的意义
设置android:layout_weight,比同组没有设的对象占用的面积多,android:layout_weight数值大的比android:layout_weight数值小占用面积多。