Home | History | Annotate | Download | only in notepad3
      1 /*
      2  * Copyright (C) 2008 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.demo.notepad3;
     18 
     19 import android.app.Activity;
     20 import android.database.Cursor;
     21 import android.os.Bundle;
     22 import android.view.View;
     23 import android.widget.Button;
     24 import android.widget.EditText;
     25 
     26 public class NoteEdit extends Activity {
     27 
     28     private EditText mTitleText;
     29     private EditText mBodyText;
     30     private Long mRowId;
     31     private NotesDbAdapter mDbHelper;
     32 
     33     @Override
     34     protected void onCreate(Bundle savedInstanceState) {
     35         super.onCreate(savedInstanceState);
     36         mDbHelper = new NotesDbAdapter(this);
     37         mDbHelper.open();
     38 
     39         setContentView(R.layout.note_edit);
     40         setTitle(R.string.edit_note);
     41 
     42         mTitleText = (EditText) findViewById(R.id.title);
     43         mBodyText = (EditText) findViewById(R.id.body);
     44 
     45         Button confirmButton = (Button) findViewById(R.id.confirm);
     46 
     47         mRowId = (savedInstanceState == null) ? null :
     48             (Long) savedInstanceState.getSerializable(NotesDbAdapter.KEY_ROWID);
     49 		if (mRowId == null) {
     50 			Bundle extras = getIntent().getExtras();
     51 			mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID)
     52 									: null;
     53 		}
     54 
     55 		populateFields();
     56 
     57         confirmButton.setOnClickListener(new View.OnClickListener() {
     58 
     59             public void onClick(View view) {
     60                 setResult(RESULT_OK);
     61                 finish();
     62             }
     63 
     64         });
     65     }
     66 
     67     private void populateFields() {
     68         if (mRowId != null) {
     69             Cursor note = mDbHelper.fetchNote(mRowId);
     70             startManagingCursor(note);
     71             mTitleText.setText(note.getString(
     72                     note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
     73             mBodyText.setText(note.getString(
     74                     note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
     75         }
     76     }
     77 
     78     @Override
     79     protected void onSaveInstanceState(Bundle outState) {
     80         super.onSaveInstanceState(outState);
     81         saveState();
     82         outState.putSerializable(NotesDbAdapter.KEY_ROWID, mRowId);
     83     }
     84 
     85     @Override
     86     protected void onPause() {
     87         super.onPause();
     88         saveState();
     89     }
     90 
     91     @Override
     92     protected void onResume() {
     93         super.onResume();
     94         populateFields();
     95     }
     96 
     97     private void saveState() {
     98         String title = mTitleText.getText().toString();
     99         String body = mBodyText.getText().toString();
    100 
    101         if (mRowId == null) {
    102             long id = mDbHelper.createNote(title, body);
    103             if (id > 0) {
    104                 mRowId = id;
    105             }
    106         } else {
    107             mDbHelper.updateNote(mRowId, title, body);
    108         }
    109     }
    110 
    111 }
    112