Last active
November 25, 2019 09:11
-
-
Save dwijonarko/0e5c7ee814f71f4e2fc5e25940a80731 to your computer and use it in GitHub Desktop.
Cloud FireStore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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=".NoteActivity"> | |
<EditText | |
android:id="@+id/edit_title" | |
android:layout_height="wrap_content" | |
android:layout_width="match_parent" | |
android:layout_marginBottom="10dp" | |
/> | |
<EditText | |
android:id="@+id/edit_description" | |
android:layout_height="wrap_content" | |
android:layout_width="match_parent" | |
android:layout_marginBottom="10dp" | |
/> | |
<Button android:id="@+id/btn_add" | |
android:layout_height="wrap_content" | |
android:layout_width="match_parent" | |
android:text="Add" | |
android:onClick="addNote" | |
/> | |
<ListView android:id="@+id/list_note" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
/> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vokasi.firebaseb; | |
import androidx.annotation.NonNull; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
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; | |
@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); | |
} | |
@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(); | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vokasi.firebase_a; | |
public class Note { | |
String docId; | |
String title; | |
String description; | |
public Note() { | |
} | |
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vokasi.firebase_a; | |
import androidx.annotation.NonNull; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.view.View; | |
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 NoteActivity extends AppCompatActivity { | |
FirebaseFirestore db = FirebaseFirestore.getInstance(); | |
CollectionReference note_ref = db.collection("notes"); | |
EditText edit_title; | |
EditText edit_description; | |
ListView list_note; | |
List<String> note_item = new ArrayList<>(); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_note); | |
edit_title = findViewById(R.id.edit_title); | |
edit_description = findViewById(R.id.edit_description); | |
list_note = findViewById(R.id.list_note); | |
} | |
@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(); | |
String data=""; | |
for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots){ | |
Note note = documentSnapshot.toObject(Note.class); | |
note.setDocId(documentSnapshot.getId()); | |
String documentId = note.getDocId(); | |
String title = note.getTitle(); | |
String description = note.getDescription(); | |
data += "ID: " + documentId | |
+ "\nTitle: " + title + "\nDescription: " + description + "\n\n"; | |
note_item.add(data); | |
} | |
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_selectable_list_item,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> note = new HashMap<>(); | |
note.put("title",title); | |
note.put("description",description); | |
db.collection("notes") | |
.add(note) | |
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() { | |
@Override | |
public void onSuccess(DocumentReference documentReference) { | |
edit_title.setText(""); | |
edit_description.setText(null); | |
Toast.makeText(NoteActivity.this, "Success", Toast.LENGTH_LONG).show(); | |
} | |
}) | |
.addOnFailureListener(new OnFailureListener() { | |
@Override | |
public void onFailure(@NonNull Exception e) { | |
Toast.makeText(NoteActivity.this, "Error "+e, Toast.LENGTH_LONG).show(); | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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