Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.MyViewHolder> {
- private Context context;
- private List<Note> notesList;
- public class MyViewHolder extends RecyclerView.ViewHolder {
- public TextView note;
- public TextView dot;
- public TextView timestamp;
- public MyViewHolder(View view) {
- super(view);
- note = view.findViewById(R.id.note);
- dot = view.findViewById(R.id.dot);
- timestamp = view.findViewById(R.id.timestamp);
- }
- }
- public NotesAdapter(Context context, List<Note> notesList) {
- this.context = context;
- this.notesList = notesList;
- }
- @Override
- public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
- View itemView = LayoutInflater.from(parent.getContext())
- .inflate(R.layout.note_list_row, parent, false);
- return new MyViewHolder(itemView);
- }
- @Override
- public void onBindViewHolder(MyViewHolder holder, int position) {
- Note note = notesList.get(position);
- holder.note.setText(note.getNote());
- // Displaying dot from HTML character code
- holder.dot.setText(Html.fromHtml("•"));
- // Formatting and displaying timestamp
- holder.timestamp.setText(formatDate(note.getTimestamp()));
- }
- @Override
- public int getItemCount() {
- return notesList.size();
- }
- /**
- * Formatting timestamp to `MMM d` format
- * Input: 2018-02-21 00:15:42
- * Output: Feb 21
- */
- private String formatDate(String dateStr) {
- try {
- SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date date = fmt.parse(dateStr);
- SimpleDateFormat fmtOut = new SimpleDateFormat("MMM d");
- return fmtOut.format(date);
- } catch (ParseException e) {
- }
- return "";
- }
- }
- ////////////////////////////////////////////////////////////////////
- MainActivity main;
- main.db.somequery;
- ////////////////////Dbhelper////////
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import java.util.ArrayList;
- import java.util.List;
- import info.androidhive.sqlite.database.model.Note;
- public class DatabaseHelper extends SQLiteOpenHelper {
- // Database Version
- private static final int DATABASE_VERSION = 1;
- // Database Name
- private static final String DATABASE_NAME = "notes_db";
- public DatabaseHelper(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
- // Creating Tables
- @Override
- public void onCreate(SQLiteDatabase db) {
- // create notes table
- db.execSQL(Note.CREATE_TABLE);
- }
- // Upgrading database
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- // Drop older table if existed
- db.execSQL("DROP TABLE IF EXISTS " + Note.TABLE_NAME);
- // Create tables again
- onCreate(db);
- }
- public long insertNote(String note) {
- // get writable database as we want to write data
- SQLiteDatabase db = this.getWritableDatabase();
- ContentValues values = new ContentValues();
- // `id` and `timestamp` will be inserted automatically.
- // no need to add them
- values.put(Note.COLUMN_NOTE, note);
- // insert row
- long id = db.insert(Note.TABLE_NAME, null, values);
- // close db connection
- db.close();
- // return newly inserted row id
- return id;
- }
- public Note getNote(long id) {
- // get readable database as we are not inserting anything
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.query(Note.TABLE_NAME,
- new String[]{Note.COLUMN_ID, Note.COLUMN_NOTE, Note.COLUMN_TIMESTAMP},
- Note.COLUMN_ID + "=?",
- new String[]{String.valueOf(id)}, null, null, null, null);
- if (cursor != null)
- cursor.moveToFirst();
- // prepare note object
- Note note = new Note(
- cursor.getInt(cursor.getColumnIndex(Note.COLUMN_ID)),
- cursor.getString(cursor.getColumnIndex(Note.COLUMN_NOTE)),
- cursor.getString(cursor.getColumnIndex(Note.COLUMN_TIMESTAMP)));
- // close the db connection
- cursor.close();
- return note;
- }
- public List<Note> getAllNotes() {
- List<Note> notes = new ArrayList<>();
- // Select All Query
- String selectQuery = "SELECT * FROM " + Note.TABLE_NAME + " ORDER BY " +
- Note.COLUMN_TIMESTAMP + " DESC";
- SQLiteDatabase db = this.getWritableDatabase();
- Cursor cursor = db.rawQuery(selectQuery, null);
- // looping through all rows and adding to list
- if (cursor.moveToFirst()) {
- do {
- Note note = new Note();
- note.setId(cursor.getInt(cursor.getColumnIndex(Note.COLUMN_ID)));
- note.setNote(cursor.getString(cursor.getColumnIndex(Note.COLUMN_NOTE)));
- note.setTimestamp(cursor.getString(cursor.getColumnIndex(Note.COLUMN_TIMESTAMP)));
- notes.add(note);
- } while (cursor.moveToNext());
- }
- // close db connection
- db.close();
- // return notes list
- return notes;
- }
- public int getNotesCount() {
- String countQuery = "SELECT * FROM " + Note.TABLE_NAME;
- SQLiteDatabase db = this.getReadableDatabase();
- Cursor cursor = db.rawQuery(countQuery, null);
- int count = cursor.getCount();
- cursor.close();
- // return count
- return count;
- }
- public int updateNote(Note note) {
- SQLiteDatabase db = this.getWritableDatabase();
- ContentValues values = new ContentValues();
- values.put(Note.COLUMN_NOTE, note.getNote());
- // updating row
- return db.update(Note.TABLE_NAME, values, Note.COLUMN_ID + " = ?",
- new String[]{String.valueOf(note.getId())});
- }
- public void deleteNote(Note note) {
- SQLiteDatabase db = this.getWritableDatabase();
- db.delete(Note.TABLE_NAME, Note.COLUMN_ID + " = ?",
- new String[]{String.valueOf(note.getId())});
- db.close();
- }
- }
- ////////////////////////////////////////adapter
- public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.MyViewHolder> {
- private Context context;
- private List<Note> notesList;
- public class MyViewHolder extends RecyclerView.ViewHolder {
- public TextView note;
- public TextView dot;
- public TextView timestamp;
- public MyViewHolder(View view) {
- super(view);
- note = view.findViewById(R.id.note);
- dot = view.findViewById(R.id.dot);
- timestamp = view.findViewById(R.id.timestamp);
- }
- }
- public NotesAdapter(Context context, List<Note> notesList) {
- this.context = context;
- this.notesList = notesList;
- }
- @Override
- public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
- View itemView = LayoutInflater.from(parent.getContext())
- .inflate(R.layout.note_list_row, parent, false);
- return new MyViewHolder(itemView);
- }
- @Override
- public void onBindViewHolder(MyViewHolder holder, int position) {
- Note note = notesList.get(position);
- holder.note.setText(note.getNote());
- // Displaying dot from HTML character code
- holder.dot.setText(Html.fromHtml("•"));
- // Formatting and displaying timestamp
- holder.timestamp.setText(formatDate(note.getTimestamp()));
- }
- @Override
- public int getItemCount() {
- return notesList.size();
- }
- /**
- * Formatting timestamp to `MMM d` format
- * Input: 2018-02-21 00:15:42
- * Output: Feb 21
- */
- private String formatDate(String dateStr) {
- try {
- SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date date = fmt.parse(dateStr);
- SimpleDateFormat fmtOut = new SimpleDateFormat("MMM d");
- return fmtOut.format(date);
- } catch (ParseException e) {
- }
- return "";
- }
- }
Add Comment
Please, Sign In to add comment