Advertisement
Guest User

Untitled

a guest
Aug 29th, 2014
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.24 KB | None | 0 0
  1. package com.chintan.justnotes.activities;
  2.  
  3. import android.annotation.TargetApi;
  4. import android.app.Dialog;
  5. import android.app.ListActivity;
  6. import android.content.Context;
  7. import android.database.Cursor;
  8. import android.os.AsyncTask;
  9. import android.os.Build;
  10. import android.os.Bundle;
  11. import android.view.View;
  12. import android.view.Window;
  13. import android.view.WindowManager;
  14. import android.widget.CursorAdapter;
  15. import android.widget.EditText;
  16. import android.widget.ListView;
  17. import android.widget.SimpleCursorAdapter;
  18.  
  19. import com.chintan.justnotes.FloatingActionButton;
  20. import com.chintan.justnotes.R;
  21. import com.chintan.justnotes.databases.DatabaseConnecter;
  22. import com.chintan.justnotes.ui.CustomDialog;
  23. import com.readystatesoftware.systembartint.SystemBarTintManager;
  24.  
  25. public class DisplayNotes extends ListActivity {
  26.  
  27. // Declare variables
  28. private static final String TITLE = "Title";
  29. private static final String SUMMARY = "Summary";
  30. private ListView mNoteListView;
  31. private CursorAdapter mNoteAdapter;
  32. private FloatingActionButton mFAB;
  33.  
  34. // Declare dialog variables
  35. private static final int DIALOG_ALERT = 10;
  36. public final static String TITLE_NOTES = "TitleNotes";
  37. public final static String SUMMARY_NOTES = "SummaryNotes";
  38. private Context mContext = this;
  39. private long mRowID;
  40. private EditText mTitle;
  41. private EditText mSummary;
  42.  
  43. @Override
  44. protected void onCreate(Bundle savedInstanceState) {
  45. super.onCreate(savedInstanceState);
  46.  
  47. setContentView(R.layout.display_notes);
  48.  
  49. // Set up status bar tint
  50. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  51. setTranslucentStatus(true);
  52. }
  53.  
  54. SystemBarTintManager tintManager = new SystemBarTintManager(this);
  55. tintManager.setStatusBarTintEnabled(true);
  56.  
  57. if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
  58. tintManager.setStatusBarTintResource(R.color.action_bar_color);
  59. } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
  60. tintManager.setStatusBarTintResource(R.color.action_bar_tint_color);
  61. }
  62.  
  63. // Locate ListView
  64. mNoteListView = getListView();
  65.  
  66. // Set up FAB
  67. mFAB = (FloatingActionButton) findViewById(R.id.floating_action_button);
  68. mFAB.listenTo(mNoteListView);
  69.  
  70. // Map all the titles into the title_text TextView
  71. String[] from = new String[]{
  72. TITLE, SUMMARY
  73. };
  74. int[] to = new int[]{
  75. R.id.title_text, R.id.summary_text
  76. };
  77.  
  78. // Create a SimpleCursorAdapter
  79. mNoteAdapter = new SimpleCursorAdapter(DisplayNotes.this,
  80. R.layout.note_item, null, from, to);
  81.  
  82. // Set the Adapter into SimpleCursorAdapter
  83. setListAdapter(mNoteAdapter);
  84. }
  85.  
  86. @Override
  87. protected Dialog onCreateDialog(int id) {
  88. switch (id) {
  89. case DIALOG_ALERT:
  90. // Set up the add note dialog
  91. mTitle = (EditText) findViewById(R.id.title_edit_text);
  92. mSummary = (EditText) findViewById(R.id.summary_edit_text);
  93.  
  94. // Retrieve the Row ID from DisplayNotes.java
  95. Bundle extras = getIntent().getExtras();
  96. if (extras != null) {
  97. mRowID = extras.getLong("row_id");
  98. mTitle.setText(extras.getString(TITLE_NOTES));
  99. mSummary.setText(extras.getString(SUMMARY_NOTES));
  100. }
  101.  
  102. CustomDialog.Builder mAddNoteDialog = new CustomDialog.Builder(mContext);
  103. mAddNoteDialog.title(mContext.getString(R.string.add_note));
  104. mAddNoteDialog.content(mContext.getString(R.string.add_note));
  105. mAddNoteDialog.positiveText(mContext.getString(R.string.button_save));
  106. mAddNoteDialog.negativeText(mContext.getString(R.string.button_cancel));
  107. mAddNoteDialog.positiveColor("#4285F4");
  108.  
  109. CustomDialog mAddNoteAlertDialog = mAddNoteDialog.build();
  110. mAddNoteAlertDialog.setCanceledOnTouchOutside(false);
  111. mAddNoteAlertDialog.setCancelable(false);
  112. mAddNoteAlertDialog.show();
  113. }
  114. return super.onCreateDialog(id);
  115. }
  116.  
  117. @TargetApi(19)
  118. private void setTranslucentStatus(boolean on) {
  119. Window win = getWindow();
  120. WindowManager.LayoutParams winParams = win.getAttributes();
  121. final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
  122. if (on) {
  123. winParams.flags |= bits;
  124. } else {
  125. winParams.flags &= ~bits;
  126. }
  127. win.setAttributes(winParams);
  128. }
  129.  
  130. // Save the note
  131. private void saveNote() {
  132. DatabaseConnecter dbConnector = new DatabaseConnecter(this);
  133.  
  134. if (getIntent().getExtras() == null) {
  135. // Passes the data to InsertNote in DatabaseConnector.java
  136. dbConnector.InsertNote(mTitle.getText().toString(),
  137. mSummary.getText().toString());
  138. }
  139. }
  140.  
  141. public void fabClicked(View view) {
  142. showDialog(DIALOG_ALERT);
  143. }
  144.  
  145. @Override
  146. protected void onResume() {
  147. super.onResume();
  148.  
  149. // Execute GetNotes Asynctask on return to MainActivity
  150. new GetNotes().execute((Object[]) null);
  151. }
  152.  
  153. @Override
  154. protected void onStop() {
  155. Cursor cursor = mNoteAdapter.getCursor();
  156.  
  157. // Deactivates the Cursor
  158. if (cursor != null)
  159. cursor.deactivate();
  160.  
  161. mNoteAdapter.changeCursor(null);
  162. super.onStop();
  163. }
  164.  
  165. // GetNotes AsyncTask
  166. private class GetNotes extends AsyncTask<Object, Object, Cursor> {
  167. DatabaseConnecter dbConnector = new DatabaseConnecter(DisplayNotes.this);
  168.  
  169. @Override
  170. protected Cursor doInBackground(Object... params) {
  171. // Open the database
  172. dbConnector.open();
  173.  
  174. return dbConnector.ListAllNotes();
  175. }
  176.  
  177. @Override
  178. protected void onPostExecute(Cursor result) {
  179. mNoteAdapter.changeCursor(result);
  180.  
  181. // Close Database
  182. dbConnector.close();
  183. }
  184. }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement