Back to project page TodoList.
The source code is released under:
Apache License
If you think the Android project TodoList 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 lyc.app.fragment; //from ww w . j a va2s. c o m import android.app.Activity; import android.content.ContentUris; import android.content.ContentValues; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; import android.util.Log; import android.view.ContextMenu; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ListView; import android.widget.TextView; import lyc.app.AddActivity; import lyc.app.App; import lyc.app.MainActivity; import lyc.app.R; import lyc.app.adapter.TodoCursorAdapter; import static lyc.app.App.Todo.COLUMN_STATUS; import static lyc.app.App.Todo.CONTENT_URI; import static lyc.app.App.Todo.DEFAULT_SORT_ORDER; /** * Created by ivan on 14-10-24. */ public class UnFinishedTodoFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> { private static final String TAG = "UnFinishedTodoFragment"; private static final int ADD_ACTIVITY = 11; private TextView tipsTextView; private ListView myListView; private TodoCursorAdapter adapter; @Override public void onCreate(Bundle savedInstanceState) { Log.d(TAG, "_+_+_+_+_+_+_+_+_+_+_"); super.onCreate(savedInstanceState); setHasOptionsMenu(true); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.todo_fragment, container, false); tipsTextView = (TextView) view.findViewById(R.id.tips); tipsTextView.setText(R.string.tips_no_data_for_unfinished); myListView = (ListView) view.findViewById(R.id.myListView); adapter = new TodoCursorAdapter(getActivity(), R.layout.todo_item_layout, null); myListView.setAdapter(adapter); registerForContextMenu(myListView); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); getActivity().getSupportLoaderManager().initLoader(0, null, this); } @Override public Loader<Cursor> onCreateLoader(int i, Bundle bundle) { return new CursorLoader(getActivity(), CONTENT_URI, null, COLUMN_STATUS + " = ?", new String[]{String.valueOf(App.Todo.STATUS_UNFINISHED)}, DEFAULT_SORT_ORDER); } @Override public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor data) { if (data.getCount() > 0) { myListView.setVisibility(View.VISIBLE); tipsTextView.setVisibility(View.GONE); } else { myListView.setVisibility(View.GONE); tipsTextView.setVisibility(View.VISIBLE); } adapter.changeCursor(data); } @Override public void onLoaderReset(Loader<Cursor> cursorLoader) { adapter.changeCursor(null); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.setHeaderTitle(R.string.finished_dialog_title); menu.add(0, 0, 0, R.string.button_positive_label); menu.add(0, 1, 1, R.string.button_negative_label); } @Override public boolean onContextItemSelected(MenuItem item) { AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); switch (item.getItemId()) { case 0: updateTodo(info.id); return true; case 1: return true; default: return super.onContextItemSelected(item); } } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); inflater.inflate(R.menu.main, menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menuAdd: Intent intent = new Intent(getActivity(), AddActivity.class); startActivityForResult(intent, ADD_ACTIVITY); return true; default: return super.onOptionsItemSelected(item); } } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == ADD_ACTIVITY) { if (resultCode == Activity.RESULT_OK) { getActivity().getSupportLoaderManager().restartLoader(0, null, this); Log.d(TAG, "============== add activity save button click =============="); } else { Log.d(TAG, "-------------- add activity back button click --------------"); } } } private void updateTodo(long id) { ContentValues cv = new ContentValues(); cv.put(App.Todo.COLUMN_STATUS, String.valueOf(App.Todo.STATUS_FINISHED)); cv.put(App.Todo.COLUMN_ACTUAL_FINISHED_DATE, System.currentTimeMillis()); Uri uri = ContentUris.withAppendedId(App.Todo.CONTENT_ID_URI_BASE, id); int count = getActivity().getContentResolver().update(uri, cv, null, null); if (count > 0) { getActivity().getSupportLoaderManager().restartLoader(0, null, this); ((MainActivity) getActivity()).refreshFinishedTodo(true); } } }