Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.chintan.justnotes.activities;
- import android.annotation.TargetApi;
- import android.app.Dialog;
- import android.app.ListActivity;
- import android.content.Context;
- import android.database.Cursor;
- import android.os.AsyncTask;
- import android.os.Build;
- import android.os.Bundle;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- import android.widget.CursorAdapter;
- import android.widget.EditText;
- import android.widget.ListView;
- import android.widget.SimpleCursorAdapter;
- import com.chintan.justnotes.FloatingActionButton;
- import com.chintan.justnotes.R;
- import com.chintan.justnotes.databases.DatabaseConnecter;
- import com.chintan.justnotes.ui.CustomDialog;
- import com.readystatesoftware.systembartint.SystemBarTintManager;
- public class DisplayNotes extends ListActivity {
- // Declare variables
- private static final String TITLE = "Title";
- private static final String SUMMARY = "Summary";
- private ListView mNoteListView;
- private CursorAdapter mNoteAdapter;
- private FloatingActionButton mFAB;
- // Declare dialog variables
- private static final int DIALOG_ALERT = 10;
- public final static String TITLE_NOTES = "TitleNotes";
- public final static String SUMMARY_NOTES = "SummaryNotes";
- private Context mContext = this;
- private long mRowID;
- private EditText mTitle;
- private EditText mSummary;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.display_notes);
- // Set up status bar tint
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
- setTranslucentStatus(true);
- }
- SystemBarTintManager tintManager = new SystemBarTintManager(this);
- tintManager.setStatusBarTintEnabled(true);
- if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
- tintManager.setStatusBarTintResource(R.color.action_bar_color);
- } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
- tintManager.setStatusBarTintResource(R.color.action_bar_tint_color);
- }
- // Locate ListView
- mNoteListView = getListView();
- // Set up FAB
- mFAB = (FloatingActionButton) findViewById(R.id.floating_action_button);
- mFAB.listenTo(mNoteListView);
- // Map all the titles into the title_text TextView
- String[] from = new String[]{
- TITLE, SUMMARY
- };
- int[] to = new int[]{
- R.id.title_text, R.id.summary_text
- };
- // Create a SimpleCursorAdapter
- mNoteAdapter = new SimpleCursorAdapter(DisplayNotes.this,
- R.layout.note_item, null, from, to);
- // Set the Adapter into SimpleCursorAdapter
- setListAdapter(mNoteAdapter);
- }
- @Override
- protected Dialog onCreateDialog(int id) {
- switch (id) {
- case DIALOG_ALERT:
- // Set up the add note dialog
- mTitle = (EditText) findViewById(R.id.title_edit_text);
- mSummary = (EditText) findViewById(R.id.summary_edit_text);
- // Retrieve the Row ID from DisplayNotes.java
- Bundle extras = getIntent().getExtras();
- if (extras != null) {
- mRowID = extras.getLong("row_id");
- mTitle.setText(extras.getString(TITLE_NOTES));
- mSummary.setText(extras.getString(SUMMARY_NOTES));
- }
- CustomDialog.Builder mAddNoteDialog = new CustomDialog.Builder(mContext);
- mAddNoteDialog.title(mContext.getString(R.string.add_note));
- mAddNoteDialog.content(mContext.getString(R.string.add_note));
- mAddNoteDialog.positiveText(mContext.getString(R.string.button_save));
- mAddNoteDialog.negativeText(mContext.getString(R.string.button_cancel));
- mAddNoteDialog.positiveColor("#4285F4");
- CustomDialog mAddNoteAlertDialog = mAddNoteDialog.build();
- mAddNoteAlertDialog.setCanceledOnTouchOutside(false);
- mAddNoteAlertDialog.setCancelable(false);
- mAddNoteAlertDialog.show();
- }
- return super.onCreateDialog(id);
- }
- @TargetApi(19)
- private void setTranslucentStatus(boolean on) {
- Window win = getWindow();
- WindowManager.LayoutParams winParams = win.getAttributes();
- final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
- if (on) {
- winParams.flags |= bits;
- } else {
- winParams.flags &= ~bits;
- }
- win.setAttributes(winParams);
- }
- // Save the note
- private void saveNote() {
- DatabaseConnecter dbConnector = new DatabaseConnecter(this);
- if (getIntent().getExtras() == null) {
- // Passes the data to InsertNote in DatabaseConnector.java
- dbConnector.InsertNote(mTitle.getText().toString(),
- mSummary.getText().toString());
- }
- }
- public void fabClicked(View view) {
- showDialog(DIALOG_ALERT);
- }
- @Override
- protected void onResume() {
- super.onResume();
- // Execute GetNotes Asynctask on return to MainActivity
- new GetNotes().execute((Object[]) null);
- }
- @Override
- protected void onStop() {
- Cursor cursor = mNoteAdapter.getCursor();
- // Deactivates the Cursor
- if (cursor != null)
- cursor.deactivate();
- mNoteAdapter.changeCursor(null);
- super.onStop();
- }
- // GetNotes AsyncTask
- private class GetNotes extends AsyncTask<Object, Object, Cursor> {
- DatabaseConnecter dbConnector = new DatabaseConnecter(DisplayNotes.this);
- @Override
- protected Cursor doInBackground(Object... params) {
- // Open the database
- dbConnector.open();
- return dbConnector.ListAllNotes();
- }
- @Override
- protected void onPostExecute(Cursor result) {
- mNoteAdapter.changeCursor(result);
- // Close Database
- dbConnector.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement