How to retrieve data from Firebase Cloud Firestore in Android studio | Quiz App | Java | Android

In this blog post, we are going to see how to retrieve data from the Cloud Firestore in the android app. Before going to start, let’s see what Cloud Firestore is.

What is Cloud Firestore?

What you’re looking for

Here I create a Quiz app using cloud firestore. Demo video:

Create a Cloud Firestore database

Test mode

Locked mode

Here I select Test Mode, but I changed my rule a little bit. When a user logged into my app only that user will access my database. So you can change your rules according to your requirements.

Warning: After you set your project’s default GCP resource location, you cannot change it.

Set up environment

Add the required dependencies and client libraries to your app.

dependencies {
implementation ‘com.google.firebase:firebase-analytics:17.3.0’
implementation ‘com.google.firebase:firebase-firestore:21.4.2’
}

Add data

My Firestore database
Hierarchy

Initialize Cloud Firestore

Initialize an instance of Cloud Firestore:

public class SplashActivity extends AppCompatActivity {
private FirebaseFirestore firestore;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.
activity_splash);
firestore = FirebaseFirestore.
getInstance();
}

}

Read Data

Now use “get” method to retrieve the entire collection.

firestore.collection(“QUIZ”).document(“Categories”)
.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()){
DocumentSnapshot doc = task.getResult();

if (doc.exists()){
long count = (long)doc.get(“COUNT”);
for (int i=1; i<=count; i++){
String catName = doc.getString(“CAT” + String.
valueOf(i));
catList.add(catName);
}
Intent intent = new Intent(SplashActivity.this, SignIn.class);
startActivity(intent);
SplashActivity.this.finish();
}else {
Toast.
makeText(SplashActivity.this, “No Category document exists”, Toast.LENGTH_SHORT).show();
finish();
}
}else
{
Toast.
makeText(SplashActivity.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});

firestore.collection(“QUIZ”).document(“CAT” + String.valueOf(category_id))
.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()){
DocumentSnapshot doc = task.getResult();
if (doc.exists()){
long sets = (long)doc.get(“SETS”);
SetsGridAdapter adapter = new SetsGridAdapter((int)sets);
setGridView.setAdapter(adapter);
}else {
Toast.
makeText(SetsActivity.this, “No CAT document exists”, Toast.LENGTH_SHORT).show();
finish();
}
}else
{
Toast.
makeText(SetsActivity.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
loadingDialog.cancel();
}
});

firestore.collection(“QUIZ”).document(“CAT” + String.valueOf(category_id))
.collection(“SET” + String.
valueOf(setNo))
.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()){
QuerySnapshot questions = task.getResult();

for (QueryDocumentSnapshot doc : questions){
questionList.add(new Question(doc.getString(“QUESTION”),
doc.getString(“A”),
doc.getString(“B”),
doc.getString(“C”),
doc.getString(“D”),
Integer.
valueOf(doc.getString(“ANSWER”))
));
}
setQuestion();
}else
{
Toast.
makeText(QuestionsActivity.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
loadingDialog.cancel();
}
});

For full code visit my GitHub account

https://github.com/barmangolap15/
https://github.com/barmangolap15/
https://github.com/barmangolap15/

Hi everyone, myself Golap an Android app developer with UI/UX designer.