@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.diary_home); mDbHelper = new DiaryDbAdapter(this); mDbHelper.open(); renderListView(); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mDbHelper = new DiaryDbAdapter(this); mDbHelper.open(); setContentView(R.layout.discovery_edit); mTitleText = (EditText) findViewById(R.id.title); mBodyText = (EditText) findViewById(R.id.body); Button confirmButton = (Button) findViewById(R.id.confirm); Button cancelButton = (Button) findViewById(R.id.beiwang_cancel); mRowId = null; // ÿһ��intent�����һ��Bundle�͵�extras���ݡ� Bundle extras = getIntent().getExtras(); if (extras != null) { String title = extras.getString(DiaryDbAdapter.KEY_TITLE); String body = extras.getString(DiaryDbAdapter.KEY_BODY); mRowId = extras.getLong(DiaryDbAdapter.KEY_ROWID); if (title != null) { mTitleText.setText(title); } if (body != null) { mBodyText.setText(body); } } final Toast t = Toast.makeText(this, "标题不能为空", Toast.LENGTH_LONG); confirmButton.setOnClickListener( new View.OnClickListener() { public void onClick(View view) { String title = mTitleText.getText().toString(); String body = mBodyText.getText().toString(); if (title.isEmpty()) { t.show(); finish(); } else { if (mRowId != null) { mDbHelper.updateDiary(mRowId, title, body); } else mDbHelper.createDiary(title, body); Intent mIntent = new Intent(); setResult(RESULT_OK, mIntent); finish(); } } }); cancelButton.setOnClickListener( new View.OnClickListener() { public void onClick(View view) { finish(); } }); }
private void renderListView() { // TODO Auto-generated method stub mDiaryCursor = mDbHelper.getAllNotes(); startManagingCursor(mDiaryCursor); String[] from = new String[] {DiaryDbAdapter.KEY_TITLE, DiaryDbAdapter.KEY_CREATED}; int[] to = new int[] {R.id.text, R.id.created}; SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.row, mDiaryCursor, from, to); setListAdapter(notes); }
public boolean onMenuItemSelected(int featureId, MenuItem item) { switch (item.getItemId()) { case INSERT_ID: create_diary(); return true; case DELETE_ID: mDbHelper.deleteDiary(getListView().getSelectedItemId()); renderListView(); return true; } return super.onMenuItemSelected(featureId, item); }