首页 > 解决方案 > 我需要显示存储在 cloud firestore android studio 中的数据

问题描述

我正在尝试在回收站视图中显示存储在云 Firestore 中的数据,但它不起作用。它没有显示任何东西。我只有一个空白活动。我的代码有什么问题?先感谢您

这是我的代码:

用户.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();
}

}

活动用户.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>

用户模型.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;
}


}

用户适配器.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);
    }
}

}

用户详细信息.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>

标签: javaandroidfirebasegoogle-cloud-firestore

解决方案


第一个错误由@MayurGajra 解决,这是我在夸大activity_users 而不是userdetails。第二个错误是 Model.java 和 firebase 中的字段名称应该相同。如果它们不同,则不会检索数据


推荐阅读