Android Coding
Android Coding

Reputation: 31

I need to display data stored in cloud firestore android studio

I am trying to display data stored in cloud firestore in a recycler view but it is not working. It is not displaying anything. I just have a blank activity. what could be wrong in my code? Thank you in advance

Here is my code:

Users.java:

package com.example.how;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
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;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;

public class Users extends AppCompatActivity {


private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference UsersRef = db.collection("Users");
private UsersAdapter adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_users);

    setUpRecyclerView();
}
private void setUpRecyclerView() {
    Query query = UsersRef;
    FirestoreRecyclerOptions<UsersModel> options = new FirestoreRecyclerOptions.Builder<UsersModel>()
            .setQuery(query, UsersModel.class)
            .build();
    adapter = new UsersAdapter(options);
    RecyclerView recyclerView = findViewById(R.id.FireStoreList);
    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();
}

}

activity_users.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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=".Users">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/FireStoreList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" >


</androidx.recyclerview.widget.RecyclerView>

</androidx.constraintlayout.widget.ConstraintLayout>

UsersModel.java:

package com.example.how;


public class UsersModel {

private String FName,LName,DOB,email,PhoneNumber,UserID;


private UsersModel(){}


private UsersModel(String FName,String LName, String DOB, String email, String PhoneNumber, 
String UserID){

    this.FName = FName;
    this.LName = LName;
    this.DOB = DOB;
    this.email = email;
    this.PhoneNumber = PhoneNumber;
    this.UserID = UserID;


}

public String getFName() {
    return FName;
}

public void setFName(String FName) {
    this.FName = FName;
}

public String getLName() {
    return LName;
}

public void setLName(String LName) {
    this.LName = LName;
}

public String getDOB() {
    return DOB;
}

public void setDOB(String DOB) {
    this.DOB = DOB;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPhoneNumber() {
    return PhoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
    PhoneNumber = phoneNumber;
}

public String getUserID() {
    return UserID;
}

public void setUserID(String userID) {
    UserID = userID;
}


}

UsersAdapter.java:

package com.example.how;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;

public class UsersAdapter extends FirestoreRecyclerAdapter<UsersModel, UsersAdapter.UsersHolder> 
{


public UsersAdapter(@NonNull FirestoreRecyclerOptions<UsersModel> options) {
    super(options);
}

@Override
protected void onBindViewHolder(@NonNull UsersHolder holder, int position, @NonNull UsersModel 
model) {

    holder.FName.setText(model.getFName());
    holder.LName.setText(model.getLName());
    holder.email.setText(model.getEmail());
    holder.PhoneNumber.setText(model.getPhoneNumber());
    holder.DOB.setText(model.getDOB());
    holder.UserID.setText(model.getUserID());

}

@NonNull
@Override
public UsersHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View v = 
LayoutInflater.from(parent.getContext()).inflate(R.layout.userdetails,parent,false);


    return new UsersHolder(v);
}

class UsersHolder extends RecyclerView.ViewHolder{

    TextView FName,LName,DOB,email,PhoneNumber,UserID;
    public UsersHolder(View itemView) {
        super(itemView);

        FName= itemView.findViewById(R.id.txtFName);
        LName= itemView.findViewById(R.id.txtLName);
        DOB= itemView.findViewById(R.id.txtDOB);
        email= itemView.findViewById(R.id.txtemail);
        PhoneNumber= itemView.findViewById(R.id.txtPhoneNumber);
        UserID= itemView.findViewById(R.id.txtUserID);
    }
}

}

userdetails.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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="wrap_content">

<TextView
    android:id="@+id/txtFName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="51dp"
    android:text="hrllo"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/txtLName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="13dp"
    android:text="hrllo"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/txtFName" />

<TextView
    android:id="@+id/txtDOB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="13dp"
    android:text="hrllo"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/txtLName" />

<TextView
    android:id="@+id/txtemail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="11dp"
    android:text="hrllo"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/txtDOB" />

<TextView
    android:id="@+id/txtPhoneNumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="11dp"
    android:text="hrllo"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/txtemail" />

<TextView
    android:id="@+id/txtUserID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="11dp"
    android:text="hrllo"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/txtPhoneNumber" />
</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 1

Views: 198

Answers (1)

Android Coding
Android Coding

Reputation: 31

The first error was solved by @MayurGajra which was that I was inflating activity_users instead of userdetails. And the second error was that fields' names should be the same in the Model.java, and the firebase. If they were different, data would not be retrieved

Upvotes: 1

Related Questions