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