ali50mahmoud

db

Mar 20th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.60 KB | None | 0 0
  1. public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.MyViewHolder> {
  2.  
  3. private Context context;
  4. private List<Note> notesList;
  5.  
  6. public class MyViewHolder extends RecyclerView.ViewHolder {
  7. public TextView note;
  8. public TextView dot;
  9. public TextView timestamp;
  10.  
  11. public MyViewHolder(View view) {
  12. super(view);
  13. note = view.findViewById(R.id.note);
  14. dot = view.findViewById(R.id.dot);
  15. timestamp = view.findViewById(R.id.timestamp);
  16. }
  17. }
  18.  
  19.  
  20. public NotesAdapter(Context context, List<Note> notesList) {
  21. this.context = context;
  22. this.notesList = notesList;
  23. }
  24.  
  25. @Override
  26. public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  27. View itemView = LayoutInflater.from(parent.getContext())
  28. .inflate(R.layout.note_list_row, parent, false);
  29.  
  30. return new MyViewHolder(itemView);
  31. }
  32.  
  33. @Override
  34. public void onBindViewHolder(MyViewHolder holder, int position) {
  35. Note note = notesList.get(position);
  36.  
  37. holder.note.setText(note.getNote());
  38.  
  39. // Displaying dot from HTML character code
  40. holder.dot.setText(Html.fromHtml("&#8226;"));
  41.  
  42. // Formatting and displaying timestamp
  43. holder.timestamp.setText(formatDate(note.getTimestamp()));
  44. }
  45.  
  46. @Override
  47. public int getItemCount() {
  48. return notesList.size();
  49. }
  50.  
  51. /**
  52. * Formatting timestamp to `MMM d` format
  53. * Input: 2018-02-21 00:15:42
  54. * Output: Feb 21
  55. */
  56. private String formatDate(String dateStr) {
  57. try {
  58. SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  59. Date date = fmt.parse(dateStr);
  60. SimpleDateFormat fmtOut = new SimpleDateFormat("MMM d");
  61. return fmtOut.format(date);
  62. } catch (ParseException e) {
  63.  
  64. }
  65.  
  66. return "";
  67. }
  68. }
  69. ////////////////////////////////////////////////////////////////////
  70. MainActivity main;
  71.  
  72. main.db.somequery;
  73. ////////////////////Dbhelper////////
  74. import android.content.ContentValues;
  75. import android.content.Context;
  76. import android.database.Cursor;
  77. import android.database.sqlite.SQLiteDatabase;
  78. import android.database.sqlite.SQLiteOpenHelper;
  79.  
  80. import java.util.ArrayList;
  81. import java.util.List;
  82.  
  83. import info.androidhive.sqlite.database.model.Note;
  84.  
  85. public class DatabaseHelper extends SQLiteOpenHelper {
  86.  
  87. // Database Version
  88. private static final int DATABASE_VERSION = 1;
  89.  
  90. // Database Name
  91. private static final String DATABASE_NAME = "notes_db";
  92.  
  93.  
  94. public DatabaseHelper(Context context) {
  95. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  96. }
  97.  
  98. // Creating Tables
  99. @Override
  100. public void onCreate(SQLiteDatabase db) {
  101.  
  102. // create notes table
  103. db.execSQL(Note.CREATE_TABLE);
  104. }
  105.  
  106. // Upgrading database
  107. @Override
  108. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  109. // Drop older table if existed
  110. db.execSQL("DROP TABLE IF EXISTS " + Note.TABLE_NAME);
  111.  
  112. // Create tables again
  113. onCreate(db);
  114. }
  115.  
  116. public long insertNote(String note) {
  117. // get writable database as we want to write data
  118. SQLiteDatabase db = this.getWritableDatabase();
  119.  
  120. ContentValues values = new ContentValues();
  121. // `id` and `timestamp` will be inserted automatically.
  122. // no need to add them
  123. values.put(Note.COLUMN_NOTE, note);
  124.  
  125. // insert row
  126. long id = db.insert(Note.TABLE_NAME, null, values);
  127.  
  128. // close db connection
  129. db.close();
  130.  
  131. // return newly inserted row id
  132. return id;
  133. }
  134.  
  135. public Note getNote(long id) {
  136. // get readable database as we are not inserting anything
  137. SQLiteDatabase db = this.getReadableDatabase();
  138.  
  139. Cursor cursor = db.query(Note.TABLE_NAME,
  140. new String[]{Note.COLUMN_ID, Note.COLUMN_NOTE, Note.COLUMN_TIMESTAMP},
  141. Note.COLUMN_ID + "=?",
  142. new String[]{String.valueOf(id)}, null, null, null, null);
  143.  
  144. if (cursor != null)
  145. cursor.moveToFirst();
  146.  
  147. // prepare note object
  148. Note note = new Note(
  149. cursor.getInt(cursor.getColumnIndex(Note.COLUMN_ID)),
  150. cursor.getString(cursor.getColumnIndex(Note.COLUMN_NOTE)),
  151. cursor.getString(cursor.getColumnIndex(Note.COLUMN_TIMESTAMP)));
  152.  
  153. // close the db connection
  154. cursor.close();
  155.  
  156. return note;
  157. }
  158.  
  159. public List<Note> getAllNotes() {
  160. List<Note> notes = new ArrayList<>();
  161.  
  162. // Select All Query
  163. String selectQuery = "SELECT * FROM " + Note.TABLE_NAME + " ORDER BY " +
  164. Note.COLUMN_TIMESTAMP + " DESC";
  165.  
  166. SQLiteDatabase db = this.getWritableDatabase();
  167. Cursor cursor = db.rawQuery(selectQuery, null);
  168.  
  169. // looping through all rows and adding to list
  170. if (cursor.moveToFirst()) {
  171. do {
  172. Note note = new Note();
  173. note.setId(cursor.getInt(cursor.getColumnIndex(Note.COLUMN_ID)));
  174. note.setNote(cursor.getString(cursor.getColumnIndex(Note.COLUMN_NOTE)));
  175. note.setTimestamp(cursor.getString(cursor.getColumnIndex(Note.COLUMN_TIMESTAMP)));
  176.  
  177. notes.add(note);
  178. } while (cursor.moveToNext());
  179. }
  180.  
  181. // close db connection
  182. db.close();
  183.  
  184. // return notes list
  185. return notes;
  186. }
  187.  
  188. public int getNotesCount() {
  189. String countQuery = "SELECT * FROM " + Note.TABLE_NAME;
  190. SQLiteDatabase db = this.getReadableDatabase();
  191. Cursor cursor = db.rawQuery(countQuery, null);
  192.  
  193. int count = cursor.getCount();
  194. cursor.close();
  195.  
  196.  
  197. // return count
  198. return count;
  199. }
  200.  
  201. public int updateNote(Note note) {
  202. SQLiteDatabase db = this.getWritableDatabase();
  203.  
  204. ContentValues values = new ContentValues();
  205. values.put(Note.COLUMN_NOTE, note.getNote());
  206.  
  207. // updating row
  208. return db.update(Note.TABLE_NAME, values, Note.COLUMN_ID + " = ?",
  209. new String[]{String.valueOf(note.getId())});
  210. }
  211.  
  212. public void deleteNote(Note note) {
  213. SQLiteDatabase db = this.getWritableDatabase();
  214. db.delete(Note.TABLE_NAME, Note.COLUMN_ID + " = ?",
  215. new String[]{String.valueOf(note.getId())});
  216. db.close();
  217. }
  218. }
  219. ////////////////////////////////////////adapter
  220. public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.MyViewHolder> {
  221.  
  222. private Context context;
  223. private List<Note> notesList;
  224.  
  225. public class MyViewHolder extends RecyclerView.ViewHolder {
  226. public TextView note;
  227. public TextView dot;
  228. public TextView timestamp;
  229.  
  230. public MyViewHolder(View view) {
  231. super(view);
  232. note = view.findViewById(R.id.note);
  233. dot = view.findViewById(R.id.dot);
  234. timestamp = view.findViewById(R.id.timestamp);
  235. }
  236. }
  237.  
  238.  
  239. public NotesAdapter(Context context, List<Note> notesList) {
  240. this.context = context;
  241. this.notesList = notesList;
  242. }
  243.  
  244. @Override
  245. public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  246. View itemView = LayoutInflater.from(parent.getContext())
  247. .inflate(R.layout.note_list_row, parent, false);
  248.  
  249. return new MyViewHolder(itemView);
  250. }
  251.  
  252. @Override
  253. public void onBindViewHolder(MyViewHolder holder, int position) {
  254. Note note = notesList.get(position);
  255.  
  256. holder.note.setText(note.getNote());
  257.  
  258. // Displaying dot from HTML character code
  259. holder.dot.setText(Html.fromHtml("&#8226;"));
  260.  
  261. // Formatting and displaying timestamp
  262. holder.timestamp.setText(formatDate(note.getTimestamp()));
  263. }
  264.  
  265. @Override
  266. public int getItemCount() {
  267. return notesList.size();
  268. }
  269.  
  270. /**
  271. * Formatting timestamp to `MMM d` format
  272. * Input: 2018-02-21 00:15:42
  273. * Output: Feb 21
  274. */
  275. private String formatDate(String dateStr) {
  276. try {
  277. SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  278. Date date = fmt.parse(dateStr);
  279. SimpleDateFormat fmtOut = new SimpleDateFormat("MMM d");
  280. return fmtOut.format(date);
  281. } catch (ParseException e) {
  282.  
  283. }
  284.  
  285. return "";
  286. }
  287. }
Add Comment
Please, Sign In to add comment