Skip to content

Instantly share code, notes, and snippets.

@dwijonarko
Created November 28, 2019 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwijonarko/ffcf34844a6f1e172fb7428a0005d512 to your computer and use it in GitHub Desktop.
Save dwijonarko/ffcf34844a6f1e172fb7428a0005d512 to your computer and use it in GitHub Desktop.
Cloud Firestore CRUD / Create Read Update Delete
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:padding="16dp"
tools:context=".FirestoreActivity">
<EditText
android:id="@+id/edit_title"
android:hint="Title"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_description"
android:hint="Description"
android:layout_marginBottom="10dp"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_add"
android:text="Add"
android:onClick="addNote" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_update"
android:text="Edit"
android:onClick="editNote" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_delete"
android:text="Delete"
android:onClick="deleteNote" />
</LinearLayout>
<ListView
android:id="@+id/list_note"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
</LinearLayout>
package com.vokasi.firebaseb;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
public class FirestoreActivity extends AppCompatActivity {
FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference note_ref = db.collection("notes");
EditText edit_title;
EditText edit_description;
ListView list_note;
ArrayList<Note> note_item = new ArrayList<>();
NoteAdapter adapter;
private String selectedId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_firestore);
edit_title = findViewById(R.id.edit_title);
edit_description = findViewById(R.id.edit_description);
list_note = findViewById(R.id.list_note);
list_note.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Note note = adapter.getItem(position);
edit_title.setText(note.getTitle());
edit_description.setText(note.getDescription());
selectedId = note.getDocId();
}
});
}
@Override
protected void onStart() {
super.onStart();
note_ref.addSnapshotListener(this, new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
if (e != null){
return;
}
note_item.clear();
for(QueryDocumentSnapshot documentSnapshot: queryDocumentSnapshots){
Note note = documentSnapshot.toObject(Note.class);
note.setDocId(documentSnapshot.getId());
note_item.add(note);
}
adapter = new NoteAdapter(FirestoreActivity.this,note_item);
adapter.notifyDataSetChanged();
list_note.setAdapter(adapter);
}
});
}
public void addNote(View view){
String title = edit_title.getText().toString();
String description = edit_description.getText().toString();
Map<String, Object> user = new HashMap<>();
user.put("title", title);
user.put("description", description);
note_ref.add(user)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
edit_title.setText(null);
edit_description.setText(null);
Toast.makeText(FirestoreActivity.this, "Success", Toast.LENGTH_LONG).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(FirestoreActivity.this, "Error"+e, Toast.LENGTH_LONG).show();
}
});
}
public void editNote(View view){
String title = edit_title.getText().toString();
String description = edit_description.getText().toString();
Note note = new Note(title,description);
note_ref.document(selectedId).set(note)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
edit_title.setText(null);
edit_description.setText(null);
Toast.makeText(FirestoreActivity.this, "Updated ", Toast.LENGTH_LONG).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(FirestoreActivity.this, "Error "+e, Toast.LENGTH_LONG).show();
}
});
}
public void deleteNote(View view){
note_ref.document(selectedId).delete()
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
edit_title.setText(null);
edit_description.setText(null);
Toast.makeText(FirestoreActivity.this, "Deleted ", Toast.LENGTH_LONG).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(FirestoreActivity.this, "Error "+e, Toast.LENGTH_LONG).show();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/note_id"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView android:id="@+id/note_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="@id/note_id"
android:layout_alignParentRight="true"
android:textAlignment="textEnd"
/>
<TextView android:id="@+id/note_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/note_id"
/>
</RelativeLayout>
package com.vokasi.firebaseb;
public class Note {
String docId;
String title;
String description;
public Note() {
}
public Note(String title, String description) {
this.title = title;
this.description = description;
}
public String getDocId() {
return docId;
}
public void setDocId(String docId) {
this.docId = docId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
package com.vokasi.firebaseb;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.ArrayList;
public class NoteAdapter extends ArrayAdapter<Note> {
public NoteAdapter(Context context,ArrayList<Note> notes) {
super(context, 0, notes);
}
public View getView(int position,View convertView, ViewGroup parent){
if (convertView ==null){
convertView = ((Activity)getContext()).getLayoutInflater().inflate(R.layout.list_note,parent,false);
}
TextView note_id = (TextView) convertView.findViewById(R.id.note_id);
TextView note_title = (TextView) convertView.findViewById(R.id.note_title);
TextView note_description = (TextView) convertView.findViewById(R.id.note_description);
Note note = getItem(position);
note_id.setText(note.getDocId());
note_title.setText(note.getTitle());
note_description.setText(note.getDescription());
return convertView;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment