Android Open Source - android_tutorial_projects Todos Overview 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;
/*w ww  .  ja  v a 2 s.  c o m*/
import android.app.ListActivity;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

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

public class TodosOverviewActivity extends ListActivity implements LoaderManager.LoaderCallbacks<Cursor> {

    private static final int ACTIVITY_CREATE = 0;
    private static final int ACTIVITY_EDIT = 1;
    private static final int DELETE_ID = Menu.FIRST + 1;

    private SimpleCursorAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.todo_list);
        this.getListView().setDividerHeight(2);
        fillData();
        registerForContextMenu(getListView());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.listmenu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId())
        {
            case R.id.insert:
                createTodo();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch( item.getItemId()) {
            case DELETE_ID:
                AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
                Uri uri = null;
                if (info != null) {
                    uri = Uri.parse(MyTodoContentProvider.CONTENT_URI + "/" + info.id);
                }
                getContentResolver().delete(uri, null, null);
                fillData();
                return true;
        }
        return super.onContextItemSelected(item);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        String[] projection = {TodoTable.COLUMN_ID, TodoTable.COLUMN_SUMMARY};
        CursorLoader cursorLoader = new CursorLoader(this, MyTodoContentProvider.CONTENT_URI, projection, null, null, null);
        return cursorLoader;
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(this, TodosOverviewActivity.class);
        Uri todoUri = Uri.parse(MyTodoContentProvider.CONTENT_URI + "/" + id);
        i.putExtra(MyTodoContentProvider.CONTENT_ITEM_TYPE, todoUri);
        startActivity(i);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
        adapter.swapCursor(cursor);

    }

    @Override
    public void onLoaderReset(Loader<Cursor> cursorLoader) {
        adapter.swapCursor(null);

    }

    private void fillData() {
        String[] from = new String[] {TodoTable.COLUMN_SUMMARY};
        int[] to  = new int[] { R.id.label};
        getLoaderManager().initLoader(0 ,null, this);
        adapter = new SimpleCursorAdapter(this, R.layout.todo_row, null, from, to, 0);
        setListAdapter(adapter);
    }

    private void createTodo() {
        Intent i = new Intent(this, TodoDetailActivity.class);
        startActivity(i);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, DELETE_ID, 0, R.string.menu_delete);
    }
}




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