Guest User

Untitled

a guest
Apr 23rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.09 KB | None | 0 0
  1. package info.androidhive.sqlite.view;
  2.  
  3. import android.content.DialogInterface;
  4. import android.os.Bundle;
  5. import android.support.design.widget.CoordinatorLayout;
  6. import android.support.design.widget.FloatingActionButton;
  7. import android.support.v7.app.AlertDialog;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.support.v7.widget.DefaultItemAnimator;
  10. import android.support.v7.widget.LinearLayoutManager;
  11. import android.support.v7.widget.RecyclerView;
  12. import android.support.v7.widget.Toolbar;
  13. import android.text.TextUtils;
  14. import android.view.LayoutInflater;
  15. import android.view.View;
  16. import android.widget.EditText;
  17. import android.widget.TextView;
  18. import android.widget.Toast;
  19.  
  20. import java.util.ArrayList;
  21. import java.util.List;
  22.  
  23. import info.androidhive.sqlite.R;
  24. import info.androidhive.sqlite.database.DatabaseHelper;
  25. import info.androidhive.sqlite.database.model.Note;
  26. import info.androidhive.sqlite.utils.MyDividerItemDecoration;
  27. import info.androidhive.sqlite.utils.RecyclerTouchListener;
  28.  
  29. public class MainActivity extends AppCompatActivity {
  30. private NotesAdapter mAdapter;
  31. private List<Note> notesList = new ArrayList<>();
  32. private CoordinatorLayout coordinatorLayout;
  33. private RecyclerView recyclerView;
  34. private TextView noNotesView;
  35.  
  36. private DatabaseHelper db;
  37.  
  38. @Override
  39. protected void onCreate(Bundle savedInstanceState) {
  40. super.onCreate(savedInstanceState);
  41. setContentView(R.layout.activity_main);
  42. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  43. setSupportActionBar(toolbar);
  44.  
  45. coordinatorLayout = findViewById(R.id.coordinator_layout);
  46. recyclerView = findViewById(R.id.recycler_view);
  47. noNotesView = findViewById(R.id.empty_notes_view);
  48.  
  49. db = new DatabaseHelper(this);
  50.  
  51. notesList.addAll(db.getAllNotes());
  52.  
  53. FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  54. fab.setOnClickListener(new View.OnClickListener() {
  55. @Override
  56. public void onClick(View view) {
  57. showNoteDialog(false, null, -1);
  58. }
  59. });
  60.  
  61. mAdapter = new NotesAdapter(this, notesList);
  62. RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
  63. recyclerView.setLayoutManager(mLayoutManager);
  64. recyclerView.setItemAnimator(new DefaultItemAnimator());
  65. recyclerView.addItemDecoration(new MyDividerItemDecoration(this, LinearLayoutManager.VERTICAL, 16));
  66. recyclerView.setAdapter(mAdapter);
  67.  
  68. toggleEmptyNotes();
  69.  
  70. /**
  71. * On long press on RecyclerView item, open alert dialog
  72. * with options to choose
  73. * Edit and Delete
  74. * */
  75. recyclerView.addOnItemTouchListener(new RecyclerTouchListener(this,
  76. recyclerView, new RecyclerTouchListener.ClickListener() {
  77. @Override
  78. public void onClick(View view, final int position) {
  79. }
  80.  
  81. @Override
  82. public void onLongClick(View view, int position) {
  83. showActionsDialog(position);
  84. }
  85. }));
  86. }
  87.  
  88. /**
  89. * Inserting new note in db
  90. * and refreshing the list
  91. */
  92. private void createNote(String note) {
  93. // inserting note in db and getting
  94. // newly inserted note id
  95. long id = db.insertNote(note);
  96.  
  97. // get the newly inserted note from db
  98. Note n = db.getNote(id);
  99.  
  100. if (n != null) {
  101. // adding new note to array list at 0 position
  102. notesList.add(0, n);
  103.  
  104. // refreshing the list
  105. mAdapter.notifyDataSetChanged();
  106.  
  107. toggleEmptyNotes();
  108. }
  109. }
  110.  
  111. /**
  112. * Updating note in db and updating
  113. * item in the list by its position
  114. */
  115. private void updateNote(String note, int position) {
  116. Note n = notesList.get(position);
  117. // updating note text
  118. n.setNote(note);
  119.  
  120. // updating note in db
  121. db.updateNote(n);
  122.  
  123. // refreshing the list
  124. notesList.set(position, n);
  125. mAdapter.notifyItemChanged(position);
  126.  
  127. toggleEmptyNotes();
  128. }
  129.  
  130. /**
  131. * Deleting note from SQLite and removing the
  132. * item from the list by its position
  133. */
  134. private void deleteNote(int position) {
  135. // deleting the note from db
  136. db.deleteNote(notesList.get(position));
  137.  
  138. // removing the note from the list
  139. notesList.remove(position);
  140. mAdapter.notifyItemRemoved(position);
  141.  
  142. toggleEmptyNotes();
  143. }
  144.  
  145. /**
  146. * Opens dialog with Edit - Delete options
  147. * Edit - 0
  148. * Delete - 0
  149. */
  150. private void showActionsDialog(final int position) {
  151. CharSequence colors[] = new CharSequence[]{"Edit", "Delete"};
  152.  
  153. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  154. builder.setTitle("Choose option");
  155. builder.setItems(colors, new DialogInterface.OnClickListener() {
  156. @Override
  157. public void onClick(DialogInterface dialog, int which) {
  158. if (which == 0) {
  159. showNoteDialog(true, notesList.get(position), position);
  160. } else {
  161. deleteNote(position);
  162. }
  163. }
  164. });
  165. builder.show();
  166. }
  167.  
  168.  
  169. /**
  170. * Shows alert dialog with EditText options to enter / edit
  171. * a note.
  172. * when shouldUpdate=true, it automatically displays old note and changes the
  173. * button text to UPDATE
  174. */
  175. private void showNoteDialog(final boolean shouldUpdate, final Note note, final int position) {
  176. LayoutInflater layoutInflaterAndroid = LayoutInflater.from(getApplicationContext());
  177. View view = layoutInflaterAndroid.inflate(R.layout.note_dialog, null);
  178.  
  179. AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(MainActivity.this);
  180. alertDialogBuilderUserInput.setView(view);
  181.  
  182. final EditText inputNote = view.findViewById(R.id.note);
  183. TextView dialogTitle = view.findViewById(R.id.dialog_title);
  184. dialogTitle.setText(!shouldUpdate ? getString(R.string.lbl_new_note_title) : getString(R.string.lbl_edit_note_title));
  185.  
  186. if (shouldUpdate && note != null) {
  187. inputNote.setText(note.getNote());
  188. }
  189. alertDialogBuilderUserInput
  190. .setCancelable(false)
  191. .setPositiveButton(shouldUpdate ? "update" : "save", new DialogInterface.OnClickListener() {
  192. public void onClick(DialogInterface dialogBox, int id) {
  193.  
  194. }
  195. })
  196. .setNegativeButton("cancel",
  197. new DialogInterface.OnClickListener() {
  198. public void onClick(DialogInterface dialogBox, int id) {
  199. dialogBox.cancel();
  200. }
  201. });
  202.  
  203. final AlertDialog alertDialog = alertDialogBuilderUserInput.create();
  204. alertDialog.show();
  205.  
  206. alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
  207. @Override
  208. public void onClick(View v) {
  209. // Show toast message when no text is entered
  210. if (TextUtils.isEmpty(inputNote.getText().toString())) {
  211. Toast.makeText(MainActivity.this, "Enter note!", Toast.LENGTH_SHORT).show();
  212. return;
  213. } else {
  214. alertDialog.dismiss();
  215. }
  216.  
  217. // check if user updating note
  218. if (shouldUpdate && note != null) {
  219. // update note by it's id
  220. updateNote(inputNote.getText().toString(), position);
  221. } else {
  222. // create new note
  223. createNote(inputNote.getText().toString());
  224. }
  225. }
  226. });
  227. }
  228.  
  229. /**
  230. * Toggling list and empty notes view
  231. */
  232. private void toggleEmptyNotes() {
  233. // you can check notesList.size() > 0
  234.  
  235. if (db.getNotesCount() > 0) {
  236. noNotesView.setVisibility(View.GONE);
  237. } else {
  238. noNotesView.setVisibility(View.VISIBLE);
  239. }
  240. }
  241. }
Add Comment
Please, Sign In to add comment