Android Open Source - android_tutorial_projects Todo Detail Activity






From Project

Back to project page android_tutorial_projects.

License

The source code is released under:

Copyright (c) 2013, Uthcode All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redi...

If you think the Android project android_tutorial_projects listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.uthcode.todos;
//from  w w  w. ja  v  a 2s  .  c o  m
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.uthcode.todos.contentprovider.MyTodoContentProvider;
import com.uthcode.todos.database.TodoTable;

public class TodoDetailActivity extends Activity {
    private Spinner mCategory;
    private EditText mTitleText;
    private EditText mBodyText;
    private Uri todoUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.todo_edit);

        mCategory = (Spinner) findViewById(R.id.category);
        mTitleText = (EditText) findViewById(R.id.todo_edit_summary);
        mBodyText = (EditText) findViewById(R.id.todo_edit_description);
        Button confirmButton = (Button) findViewById(R.id.todo_edit_button);

        Bundle extras = getIntent().getExtras();

        todoUri = (savedInstanceState == null) ? null : (Uri) savedInstanceState.getParcelable(MyTodoContentProvider.CONTENT_ITEM_TYPE);

        if (extras != null) {
            todoUri = extras.getParcelable(MyTodoContentProvider.CONTENT_ITEM_TYPE);
            fillData(todoUri);
        }

        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(mTitleText.getText().toString())) {
                    makeToast();
                } else {
                    setResult(RESULT_OK);
                    finish();
                }
            }
        });
    }

    private void fillData(Uri uri) {
        String[] projection = {TodoTable.COLUMN_SUMMARY, TodoTable.COLUMN_DESCRIPTION, TodoTable.COLUMN_CATEGORY};
        Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
            String category = cursor.getString(cursor.getColumnIndexOrThrow(TodoTable.COLUMN_CATEGORY));

            for (int i = 0; i < mCategory.getCount(); i++ ) {
                String s = (String) mCategory.getItemAtPosition(i);
                if (s != null) {
                    if (s.equalsIgnoreCase(category)) {
                        mCategory.setSelection(i);
                    }
                }
            }

            mTitleText.setText(cursor.getColumnIndexOrThrow(TodoTable.COLUMN_SUMMARY));
            mBodyText.setText(cursor.getColumnIndexOrThrow(TodoTable.COLUMN_DESCRIPTION));
            cursor.close();
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        saveState();
        outState.putParcelable(MyTodoContentProvider.CONTENT_ITEM_TYPE, todoUri);
    }

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

    private void saveState() {
        String category = (String) mCategory.getSelectedItem();
        String summary = mTitleText.getText().toString();
        String description = mBodyText.getText().toString();

        if (description.length() == 0 || summary.length() == 0) {
            return;
        }

        ContentValues values = new ContentValues();
        values.put(TodoTable.COLUMN_CATEGORY,category);
        values.put(TodoTable.COLUMN_DESCRIPTION, description);
        values.put(TodoTable.COLUMN_SUMMARY, summary);

        if (todoUri == null) {
            todoUri = getContentResolver().insert(MyTodoContentProvider.CONTENT_URI, values);
        } else {
           getContentResolver().update(todoUri,values, null, null);
        }
    }

    private void makeToast() {
        Toast.makeText(TodoDetailActivity.this, "Please maintain a summary", Toast.LENGTH_LONG).show();
    }
}




Java Source Code List

com.uthcode.Main.java
com.uthcode.alertexampleactivity.MainActivity.java
com.uthcode.asynctask.ReadWebpageAsyncTask.java
com.uthcode.exampledatepicker.MainActivity.java
com.uthcode.expandablelistview.Group.java
com.uthcode.expandablelistview.MainActivity.java
com.uthcode.expandablelistview.MyExpandableListAdapter.java
com.uthcode.imagepicker.ImagePickActivity.java
com.uthcode.implicitintents.CallIntentsActivity.java
com.uthcode.intents.MainActivity.java
com.uthcode.intents.ResultActivity.java
com.uthcode.lifecycleactivity.MainActivity.java
com.uthcode.lifecycleactivity.SecondActivity.java
com.uthcode.lifecycleactivity.TracerActivity.java
com.uthcode.mylistactivity.MyListActivity.java
com.uthcode.mylistactivity.MyPerformanceArrayAdapter.java
com.uthcode.mylistusingmodels.InteractiveArrayAdapter.java
com.uthcode.mylistusingmodels.Model.java
com.uthcode.mylistusingmodels.MyListActivity.java
com.uthcode.overview.OverviewActivity.java
com.uthcode.parsejson.MainActivity.java
com.uthcode.progresstest.MainActivity.java
com.uthcode.registeredintent.BrowserActivity.java
com.uthcode.rssreader.DetailFragment.java
com.uthcode.rssreader.MyListFragment.java
com.uthcode.rssreader.RssfeedActivity.java
com.uthcode.simplecursoradapter.MainActivity.java
com.uthcode.tempconverter.ConverterUtil.java
com.uthcode.tempconverter.MainActivity.java
com.uthcode.testdatabaseactivity.Comment.java
com.uthcode.testdatabaseactivity.CommentsDataSource.java
com.uthcode.testdatabaseactivity.MainActivity.java
com.uthcode.testdatabaseactivity.MySQLiteHelper.java
com.uthcode.threadslifecycle.ThreadsLifecycleActivity.java
com.uthcode.todolist.ToDoContentProvider.java
com.uthcode.todolist.ToDoItemAdapter.java
com.uthcode.todolist.ToDoItem.java
com.uthcode.todolist.ToDoListActivity.java
com.uthcode.todolist.ToDoListItemView.java
com.uthcode.todos.TodoDetailActivity.java
com.uthcode.todos.TodosOverviewActivity.java
com.uthcode.todos.contentprovider.MyTodoContentProvider.java
com.uthcode.todos.database.TodoDatabaseHelper.java
com.uthcode.todos.database.TodoTable.java
com.uthcode.twolistitemsactivity.MyTwoListItemsActivity.java