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 
     41         mTitleText = (EditText) findViewById(R.id.title);
     42         mBodyText = (EditText) findViewById(R.id.body);
     43 
     44         Button confirmButton = (Button) findViewById(R.id.confirm);
     45 
     46         mRowId = (savedInstanceState == null) ? null :
     47             (Long) savedInstanceState.getSerializable(NotesDbAdapter.KEY_ROWID);
     48 		if (mRowId == null) {
     49 			Bundle extras = getIntent().getExtras();
     50 			mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID)
     51 									: null;
     52 		}
     53 
     54 		populateFields();
     55 
     56         confirmButton.setOnClickListener(new View.OnClickListener() {
     57 
     58             public void onClick(View view) {
     59                 setResult(RESULT_OK);
     60                 finish();
     61             }
     62 
     63         });
     64     }
     65 
     66     private void populateFields() {
     67         if (mRowId != null) {
     68             Cursor note = mDbHelper.fetchNote(mRowId);
     69             startManagingCursor(note);
     70             mTitleText.setText(note.getString(
     71                     note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
     72             mBodyText.setText(note.getString(
     73                     note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
     74         }
     75     }
     76 
     77     @Override
     78     protected void onSaveInstanceState(Bundle outState) {
     79         super.onSaveInstanceState(outState);
     80         saveState();
     81         outState.putSerializable(NotesDbAdapter.KEY_ROWID, mRowId);
     82     }
     83 
     84     @Override
     85     protected void onPause() {
     86         super.onPause();
     87         saveState();
     88     }
     89 
     90     @Override
     91     protected void onResume() {
     92         super.onResume();
     93         populateFields();
     94     }
     95 
     96     private void saveState() {
     97         String title = mTitleText.getText().toString();
     98         String body = mBodyText.getText().toString();
     99 
    100         if (mRowId == null) {
    101             long id = mDbHelper.createNote(title, body);
    102             if (id > 0) {
    103                 mRowId = id;
    104             }
    105         } else {
    106             mDbHelper.updateNote(mRowId, title, body);
    107         }
    108     }
    109 
    110 }
    111