Android Open Source - android_tutorial_projects To Do List 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.todolist;
// w w w  .j av a 2  s .  c  o  m
import java.util.ArrayList;

import android.app.Activity;
import android.app.LoaderManager;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class ToDoListActivity extends Activity implements LoaderManager.LoaderCallbacks<Cursor> {

    private ArrayList<ToDoItem> mTodoItemsList;
    private ToDoItemAdapter mToDoItemsAdapter;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final EditText newItemEditText = (EditText) findViewById(R.id.new_item);

        newItemEditText.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN
                        && keyCode == KeyEvent.KEYCODE_ENTER) {
                    onNewItemAdded(newItemEditText.getText().toString());
                    newItemEditText.setText("");
                    return true;
                }
                return false;
            }
        });
                

        // Get references to the ListView
        final ListView todoListView = (ListView) findViewById(R.id.todos);

        // Create the array list of to do items
        mTodoItemsList = new ArrayList<ToDoItem>();

        // Create the array adapter to bind the array to the ListView
        mToDoItemsAdapter = new ToDoItemAdapter(this, R.layout.todolist_item, mTodoItemsList);

        // Bind the array adapter to the ListView.
        todoListView.setAdapter(mToDoItemsAdapter);

        // CusrorLoader ensure queries are performed asynchronously.
        getLoaderManager().initLoader(0, null, this); // // Third parameter is reference to callbacks
    }

    @Override
    protected void onResume() {
        super.onResume();
        getLoaderManager().restartLoader(0, null, this); // // Third parameter is reference to callbacks
        
        
    }

    public void onNewItemAdded(String newItem) {
        //  Each ContentValue represents a single table row
        //  as a map of column names to values.
        final ContentValues values = new ContentValues();
        values.put(ToDoContentProvider.KEY_TASK, newItem);

        // Content Provider data are consumed using a Content Resolver
        final ContentResolver cr = getContentResolver();
        // The URI specifies the content provider.
        cr.insert(ToDoContentProvider.CONTENT_URI, values);
        getLoaderManager().restartLoader(0, null, this); // Third parameter is reference to callbacks
    }
    
    public void onItemDelete(Integer item) {
      
      final ContentValues values = new ContentValues();
      values.put(ToDoContentProvider.KEY_TASK, item);
      
      final ContentResolver cr = getContentResolver();
      cr.delete(ToDoContentProvider.CONTENT_URI,ToDoContentProvider.KEY_ID + "=" + Integer.toString(item), null);
    
      getLoaderManager().restartLoader(0, null, this);
      
    }

    // Called when the loader is initliazed.
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        return new CursorLoader(this, ToDoContentProvider.CONTENT_URI,
                null, null, null, null);
    }

    // Called when the Loader Manager has completed the async query.
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        mTodoItemsList.clear();

        // Gets index of the column given a name.
        final int taskKeyIndex = cursor.getColumnIndexOrThrow(ToDoContentProvider.KEY_TASK);
        final int taskItemIndex = cursor.getColumnIndexOrThrow(ToDoContentProvider.KEY_ID);

        // Database queries are returned as Cursor objects.
        // Cursors are pointers to the result set within the underlying data.
        // Here is how we iterate over the cursor rows.
        while (cursor.moveToNext()) { // Moves cursor to next row, cursor is initialized at before first.
            final ToDoItem newItem = new ToDoItem(cursor.getString(taskKeyIndex), Integer.parseInt(cursor.getString(taskItemIndex))); // Extract column data from cursor
            mTodoItemsList.add(newItem);
        }
        mToDoItemsAdapter.notifyDataSetChanged();
    }

    public void onLoaderReset(Loader<Cursor> loader) {
    }
    
    public void callChecked(View view) {
      
      final ToDoListItemView todolist = (ToDoListItemView) view;
      onItemDelete(todolist.getItem());
      getLoaderManager().restartLoader(0, null, this);
      todolist.setChecked(false);
    
    }
}




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