Links & Dependenciesnote_item.xmlactivity_main.xmlNoteAdapter.javaNoteViewModel.javaMainActivity.javaNoteRepository.javaNoteDao.javaNoteDatabase.javaNote.java
“Adding Components” page with dependencies & instructions:
developer.android.com/topic/libraries/architecture/adding-components
CardView & Design Support Library:
developer.android.com/topic/libraries/support-library/packages#v7-cardview
developer.android.com/topic/libraries/support-library/packages#design
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp"> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp"> <TextView android:id="@+id/text_view_priority" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:text="1" android:textAppearance="@style/TextAppearance.AppCompat.Large" /> <TextView android:id="@+id/text_view_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_toStartOf="@id/text_view_priority" android:ellipsize="end" android:maxLines="1" android:text="Title" android:textAppearance="@style/TextAppearance.AppCompat.Large" /> <TextView android:id="@+id/text_view_description" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/text_view_title" android:text="Description" /> </RelativeLayout> </android.support.v7.widget.CardView>
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" tools:listitem="@layout/note_item" /> </android.support.design.widget.CoordinatorLayout>
package com.codinginflow.architectureexample; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteHolder> { private List<Note> notes = new ArrayList<>(); @NonNull @Override public NoteHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.note_item, parent, false); return new NoteHolder(itemView); } @Override public void onBindViewHolder(@NonNull NoteHolder holder, int position) { Note currentNote = notes.get(position); holder.textViewTitle.setText(currentNote.getTitle()); holder.textViewDescription.setText(currentNote.getDescription()); holder.textViewPriority.setText(String.valueOf(currentNote.getPriority())); } @Override public int getItemCount() { return notes.size(); } public void setNotes(List<Note> notes) { this.notes = notes; notifyDataSetChanged(); } class NoteHolder extends RecyclerView.ViewHolder { private TextView textViewTitle; private TextView textViewDescription; private TextView textViewPriority; public NoteHolder(View itemView) { super(itemView); textViewTitle = itemView.findViewById(R.id.text_view_title); textViewDescription = itemView.findViewById(R.id.text_view_description); textViewPriority = itemView.findViewById(R.id.text_view_priority); } } }
package com.codinginflow.architectureexample; import android.app.Application; import android.arch.lifecycle.AndroidViewModel; import android.arch.lifecycle.LiveData; import android.support.annotation.NonNull; import java.util.List; public class NoteViewModel extends AndroidViewModel { private NoteRepository repository; private LiveData<List<Note>> allNotes; public NoteViewModel(@NonNull Application application) { super(application); repository = new NoteRepository(application); allNotes = repository.getAllNotes(); } public void insert(Note note) { repository.insert(note); } public void update(Note note) { repository.update(note); } public void delete(Note note) { repository.delete(note); } public void deleteAllNotes() { repository.deleteAllNotes(); } public LiveData<List<Note>> getAllNotes() { return allNotes; } }
package com.codinginflow.architectureexample; import android.arch.lifecycle.Observer; import android.arch.lifecycle.ViewModelProviders; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import java.util.List; public class MainActivity extends AppCompatActivity { private NoteViewModel noteViewModel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); RecyclerView recyclerView = findViewById(R.id.recycler_view); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setHasFixedSize(true); final NoteAdapter adapter = new NoteAdapter(); recyclerView.setAdapter(adapter); noteViewModel = ViewModelProviders.of(this).get(NoteViewModel.class); noteViewModel.getAllNotes().observe(this, new Observer<List<Note>>() { @Override public void onChanged(@Nullable List<Note> notes) { adapter.setNotes(notes); } }); } }
package com.codinginflow.architectureexample; import android.app.Application; import android.arch.lifecycle.LiveData; import android.os.AsyncTask; import java.util.List; public class NoteRepository { private NoteDao noteDao; private LiveData<List<Note>> allNotes; public NoteRepository(Application application) { NoteDatabase database = NoteDatabase.getInstance(application); noteDao = database.noteDao(); allNotes = noteDao.getAllNotes(); } public void insert(Note note) { new InsertNoteAsyncTask(noteDao).execute(note); } public void update(Note note) { new UpdateNoteAsyncTask(noteDao).execute(note); } public void delete(Note note) { new DeleteNoteAsyncTask(noteDao).execute(note); } public void deleteAllNotes() { new DeleteAllNotesAsyncTask(noteDao).execute(); } public LiveData<List<Note>> getAllNotes() { return allNotes; } private static class InsertNoteAsyncTask extends AsyncTask<Note, Void, Void> { private NoteDao noteDao; private InsertNoteAsyncTask(NoteDao noteDao) { this.noteDao = noteDao; } @Override protected Void doInBackground(Note... notes) { noteDao.insert(notes[0]); return null; } } private static class UpdateNoteAsyncTask extends AsyncTask<Note, Void, Void> { private NoteDao noteDao; private UpdateNoteAsyncTask(NoteDao noteDao) { this.noteDao = noteDao; } @Override protected Void doInBackground(Note... notes) { noteDao.update(notes[0]); return null; } } private static class DeleteNoteAsyncTask extends AsyncTask<Note, Void, Void> { private NoteDao noteDao; private DeleteNoteAsyncTask(NoteDao noteDao) { this.noteDao = noteDao; } @Override protected Void doInBackground(Note... notes) { noteDao.delete(notes[0]); return null; } } private static class DeleteAllNotesAsyncTask extends AsyncTask<Void, Void, Void> { private NoteDao noteDao; private DeleteAllNotesAsyncTask(NoteDao noteDao) { this.noteDao = noteDao; } @Override protected Void doInBackground(Void... voids) { noteDao.deleteAllNotes(); return null; } } }
package com.codinginflow.architectureexample; import android.arch.lifecycle.LiveData; import android.arch.persistence.room.Dao; import android.arch.persistence.room.Delete; import android.arch.persistence.room.Insert; import android.arch.persistence.room.Query; import android.arch.persistence.room.Update; import java.util.List; @Dao public interface NoteDao { @Insert void insert(Note note); @Update void update(Note note); @Delete void delete(Note note); @Query("DELETE FROM note_table") void deleteAllNotes(); @Query("SELECT * FROM note_table ORDER BY priority DESC") LiveData<List<Note>> getAllNotes(); }
package com.codinginflow.architectureexample; import android.arch.persistence.db.SupportSQLiteDatabase; import android.arch.persistence.room.Database; import android.arch.persistence.room.Room; import android.arch.persistence.room.RoomDatabase; import android.content.Context; import android.os.AsyncTask; import android.support.annotation.NonNull; @Database(entities = {Note.class}, version = 1) public abstract class NoteDatabase extends RoomDatabase { private static NoteDatabase instance; public abstract NoteDao noteDao(); public static synchronized NoteDatabase getInstance(Context context) { if (instance == null) { instance = Room.databaseBuilder(context.getApplicationContext(), NoteDatabase.class, "note_database") .fallbackToDestructiveMigration() .addCallback(roomCallback) .build(); } return instance; } private static RoomDatabase.Callback roomCallback = new RoomDatabase.Callback() { @Override public void onCreate(@NonNull SupportSQLiteDatabase db) { super.onCreate(db); new PopulateDbAsyncTask(instance).execute(); } }; private static class PopulateDbAsyncTask extends AsyncTask<Void, Void, Void> { private NoteDao noteDao; private PopulateDbAsyncTask(NoteDatabase db) { noteDao = db.noteDao(); } @Override protected Void doInBackground(Void... voids) { noteDao.insert(new Note("Title 1", "Description 1", 1)); noteDao.insert(new Note("Title 2", "Description 2", 2)); noteDao.insert(new Note("Title 3", "Description 3", 3)); return null; } } }
package com.codinginflow.architectureexample; import android.arch.persistence.room.Entity; import android.arch.persistence.room.PrimaryKey; @Entity(tableName = "note_table") public class Note { @PrimaryKey(autoGenerate = true) private int id; private String title; private String description; private int priority; public Note(String title, String description, int priority) { this.title = title; this.description = description; this.priority = priority; } public void setId(int id) { this.id = id; } public int getId() { return id; } public String getTitle() { return title; } public String getDescription() { return description; } public int getPriority() { return priority; } }