Back to project page android_tutorial_projects.
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.
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); } }