[embedyt] https://www.youtube.com/watch?v=lAGI6jGS4vs[/embedyt]
Links & DependenciesNoteAdapter.javaMainActivity.javaactivity_main.xmlnote_item.xmlNote.javanew_note_menu.xml
Firebase console:
FirebaseUI GitHub page with dependencies & instructions:
github.com/firebase/FirebaseUI-Android
Other dependencies:
developer.android.com/topic/libraries/support-library/packages#v7-cardview
developer.android.com/topic/libraries/support-library/packages#design
package com.codinginflow.firebaseui_firestoreexample; 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 com.firebase.ui.firestore.FirestoreRecyclerAdapter; import com.firebase.ui.firestore.FirestoreRecyclerOptions; public class NoteAdapter extends FirestoreRecyclerAdapter<Note, NoteAdapter.NoteHolder> { public NoteAdapter(@NonNull FirestoreRecyclerOptions<Note> options) { super(options); } @Override protected void onBindViewHolder(@NonNull NoteHolder holder, int position, @NonNull Note model) { holder.textViewTitle.setText(model.getTitle()); holder.textViewDescription.setText(model.getDescription()); holder.textViewPriority.setText(String.valueOf(model.getPriority())); } @NonNull @Override public NoteHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.note_item, parent, false); return new NoteHolder(v); } class NoteHolder extends RecyclerView.ViewHolder { TextView textViewTitle; TextView textViewDescription; 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.firebaseui_firestoreexample; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import com.firebase.ui.firestore.FirestoreRecyclerOptions; import com.google.firebase.firestore.CollectionReference; import com.google.firebase.firestore.FirebaseFirestore; import com.google.firebase.firestore.Query; public class MainActivity extends AppCompatActivity { private FirebaseFirestore db = FirebaseFirestore.getInstance(); private CollectionReference notebookRef = db.collection("Notebook"); private NoteAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setUpRecyclerView(); } private void setUpRecyclerView() { Query query = notebookRef.orderBy("priority", Query.Direction.DESCENDING); FirestoreRecyclerOptions<Note> options = new FirestoreRecyclerOptions.Builder<Note>() .setQuery(query, Note.class) .build(); adapter = new NoteAdapter(options); RecyclerView recyclerView = findViewById(R.id.recycler_view); recyclerView.setHasFixedSize(true); recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setAdapter(adapter); } @Override protected void onStart() { super.onStart(); adapter.startListening(); } @Override protected void onStop() { super.onStop(); adapter.stopListening(); } }
<?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" /> <android.support.design.widget.FloatingActionButton android:id="@+id/button_add_note" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="16dp" android:src="@drawable/ic_add" /> </android.support.design.widget.CoordinatorLayout>
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" app:cardBackgroundColor="#FFFFE8"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:padding="8dp"> <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_priority" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentTop="true" android:text="1" 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_alignParentStart="true" android:layout_below="@id/text_view_title" android:text="Description" /> </RelativeLayout> </android.support.v7.widget.CardView>
package com.codinginflow.firebaseui_firestoreexample; public class Note { private String title; private String description; private int priority; public Note() { //empty constructor needed } public Note(String title, String description, int priority) { this.title = title; this.description = description; this.priority = priority; } public String getTitle() { return title; } public String getDescription() { return description; } public int getPriority() { return priority; } }
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/save_note" android:icon="@drawable/ic_save" android:title="Save" app:showAsAction="ifRoom" /> </menu>