我试图在一个回收器视图中显示存储在云firestore中的数据,但它不工作。它没有显示任何东西。我只是有一个空白的活动。我的代码可能出了什么问题?提前谢谢你
下面是我的代码: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>
第一个错误是由@MayurGajra解决的,即我正在膨胀activity_users而不是userdetails。第二个错误是在Model.java和firebase中字段的名称应该是相同的。如果它们不同,则不会检索到数据