FileDocCategorySizeDatePackage
TitleEditor.javaAPI DocGoogle Android v1.5 Example3282Sun Nov 11 13:01:04 GMT 2007com.google.android.notepad

TitleEditor.java

/* 
 * Copyright (C) 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.notepad;

import com.google.provider.NotePad;

import android.app.Activity;
import android.database.Cursor;
import android.net.ContentURI;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


/**
 * An activity that will edit the title of a note. Displays a floating
 * window with a text field.
 */
public class TitleEditor extends Activity implements View.OnClickListener {

    /**
     * This is a special intent action that means "edit the title of a note".
     */
    public static final String EDIT_TITLE_ACTION =
        "com.google.android.notepad.action.EDIT_TITLE";

    /**
     * Index of the title column
     */
    private static final int TITLE_INDEX = 1;

    /**
     * An array of the columns we are interested in.
     */
    private static final String[] PROJECTION = new String[] {
            NotePad.Notes._ID, // 0
            NotePad.Notes.TITLE, // 1
    };
    
    /**
     * Cursor which will provide access to the note whose title we are editing.
     */
    Cursor mCursor;

    /**
     * The EditText field from our UI. Keep track of this so we can extract the
     * text when we are finished.
     */
    EditText mText;
    
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        setContentView(R.layout.title_editor);

        // Get the uri of the note whose title we want to edit
        ContentURI uri = getIntent().getData();

        // Get a cursor to access the note
        mCursor = managedQuery(uri, PROJECTION, null, null);

        // Set up click handlers for the text field and button
        mText = (EditText) this.findViewById(R.id.title);
        mText.setOnClickListener(this);
        
        Button b = (Button) findViewById(R.id.ok);
        b.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();

        // Initialize the text with the title column from the cursor
        if (mCursor != null) {
            mCursor.first();
            String title = mCursor.getString(TITLE_INDEX);
            mText.setText(title);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

        // Write the text back into the cursor 
        if (mCursor != null) {
            String title = mText.getText().toString();
            mCursor.updateString(TITLE_INDEX, title);
            mCursor.commitUpdates();
        }
    }

    public void onClick(View v) {
        // When the user clicks, just finish this activity.
        // onPause will be called, and we save our data there.
        finish();
    }
}