我的回收站视图是空的,即使它包含数据
My Recycler View is empty even though it contains Data
我有一个应用程序专门从 Firestore 从 Firebase 检索数据,问题是显示数据为空的回收器视图,即使 它在另一个片段中工作同样的逻辑,我不知道我哪里错了或错了!正如我所说,该片段完全是空的,此外,如果我添加了一个 TextView 或 Button,它会正常显示,但回收器视图不会显示。
ps:当我复制相同的代码时,我更改了布局名称和回收器名称以指向片段的另一个布局和回收器视图;
这是片段
FavoriteFragment.java:处理代码的片段
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_favorite, container, false);
recyclerView = view.findViewById(R.id.fav_recycler);
linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
mAuth = FirebaseAuth.getInstance();
FirebaseUser auth_user = mAuth.getCurrentUser();
db = FirebaseFirestore.getInstance();
//Fetch Users Info
Query query = db.collection("posts")
.orderBy("posttime", Query.Direction.DESCENDING); // order the query by date
FirestoreRecyclerOptions<Posts> response = new FirestoreRecyclerOptions.Builder<Posts>()
.setQuery(query, Posts.class)
.build();
adapter = new MainAdapter(response);
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
return view;
}
fragment_favorite.xml:片段的布局。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".FavoriteFragment">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="15dp">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/fav_recycler"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="15dp"/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</FrameLayout>
和
MainAdapter.java:Recycler 视图的适配器,还处理使项目(在本例中为 post)被添加的代码或从 firestore 中删除
public class MainAdapter extends FirestoreRecyclerAdapter<Posts, MainAdapter.ViewHolder> {
/**
* Create a new RecyclerView adapter that listens to a Firestore Query. See {@link
* FirestoreRecyclerOptions} for configuration options.
*
* @param options
*/
public MainAdapter(@NonNull FirestoreRecyclerOptions options) {
super(options);
}
private FirebaseFirestore db;
private DocumentReference documentReference;
private FirebaseAuth mAuth;
boolean isthere = false;
boolean isExist = false;
@Override
protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull Posts post) {
db = FirebaseFirestore.getInstance();
mAuth = FirebaseAuth.getInstance();
documentReference = db.collection("users").document(mAuth.getUid());
holder.txtTitle.setText(post.getName());
holder.txtDesc.setText(post.getTitle() + "\n" +post.getDesc() + "\n" + post.getBloodtype() + "\n" + post.getCity() + "\n" + post.getNumber()+ "\n" + post.getDeadline());
Glide.with(holder.image.getContext()).load(post.getImage())
.into(holder.image);
holder.root.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar.make(v, post.getUserid() + "\n" + post.getName() + "\n" +post.getBloodtype() + "\n" + post.getCity(), Snackbar.LENGTH_LONG)
.setAnchorView(R.id.navigation) // Set SnackBar above the BottomNavigationView
.show();
}
});
Posts newPost = new Posts(post.getUserid(), post.getName(), post.getTitle(),post.getDesc(), post.getDeadline(), post.getNumber(),
post.getCity(), post.getBloodtype(), post.getImage(), post.getPosttime(), post.getPostid());
holder.fav.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
// Toast.makeText(buttonView.getContext(), "Added to favorite", Toast.LENGTH_SHORT).show();
if (isExist == false) {
db.collection("users").document(mAuth.getUid()).collection("favorites").document(post.getPostid())
.set(newPost)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(buttonView.getContext(), "Succeed \nBoolean: " + isExist, Toast.LENGTH_SHORT).show();
}
});
}
}
else {
// removefromfav(post.getPostid(), buttonView);
documentReference.collection("favorites").document(post.getPostid())
.delete()
.addOnSuccessListener(new OnSuccessListener<Void>() {
public void onSuccess(Void aVoid) {
// Toast.makeText(buttonView.getContext(), "Removed from favorite " + isExist, Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
}
}
});
documentReference.collection("favorites").document(post.getPostid())
.get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if(task.isSuccessful()){
isExist = task.getResult().exists();
if (isExist == true) {
holder.fav.setChecked(true);
}
}
}
});
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.users_item, parent, false);
return new ViewHolder(view);
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public LinearLayout root;
public TextView txtTitle;
public TextView txtDesc;
public ImageView image;
public ToggleButton fav;
public ViewHolder(View itemView) {
super(itemView);
root = itemView.findViewById(R.id.list_root);
txtTitle = (TextView) itemView.findViewById(R.id.list_title);
txtDesc = (TextView) itemView.findViewById(R.id.list_desc);
image = (ImageView)itemView.findViewById(R.id.list_image);
fav = (ToggleButton)itemView.findViewById(R.id.favbutton);
}
}
}
编辑:
我添加了帖子class
Posts.java:
public class Posts {
private String userid;
private String name;
private String title;
private String desc;
private String deadline;
private String number;
private String city;
private String bloodtype;
private String image;
private String posttime;
private String postid;
public Posts(){
}
public Posts(String userid,String name, String title, String desc, String deadline,
String number, String city, String bloodtype, String image, String posttime, String postid){
this.userid = userid;
this.name = name;
this.title = title;
this.desc = desc;
this.deadline = deadline;
this.number = number;
this.city = city;
this.bloodtype = bloodtype;
this.image = image;
this.posttime = posttime;
this.postid = postid;
}
public String getUserid(){
return userid;
}
public String getName(){
return name;
}
public String getTitle(){
return title;
}
public String getDesc(){
return desc;
}
public String getDeadline(){
return deadline;
}
public String getNumber(){
return number;
}
public String getCity(){
return city;
}
public String getBloodtype(){
return bloodtype;
}
public String getImage(){
return image;
}
public String getPosttime(){
return posttime;
}
public String getPostid(){
return postid;
}
}
下面是 posts 在 firestore 中的截图:
好吧,我明白了,我错过了 onStart 和 onStop 来启动和停止侦听适配器。
@Override
public void onStart() {
super.onStart();
adapter.startListening();
}
@Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
我有一个应用程序专门从 Firestore 从 Firebase 检索数据,问题是显示数据为空的回收器视图,即使 它在另一个片段中工作同样的逻辑,我不知道我哪里错了或错了!正如我所说,该片段完全是空的,此外,如果我添加了一个 TextView 或 Button,它会正常显示,但回收器视图不会显示。
ps:当我复制相同的代码时,我更改了布局名称和回收器名称以指向片段的另一个布局和回收器视图; 这是片段
FavoriteFragment.java:处理代码的片段
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_favorite, container, false);
recyclerView = view.findViewById(R.id.fav_recycler);
linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
mAuth = FirebaseAuth.getInstance();
FirebaseUser auth_user = mAuth.getCurrentUser();
db = FirebaseFirestore.getInstance();
//Fetch Users Info
Query query = db.collection("posts")
.orderBy("posttime", Query.Direction.DESCENDING); // order the query by date
FirestoreRecyclerOptions<Posts> response = new FirestoreRecyclerOptions.Builder<Posts>()
.setQuery(query, Posts.class)
.build();
adapter = new MainAdapter(response);
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
return view;
}
fragment_favorite.xml:片段的布局。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".FavoriteFragment">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="15dp">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/fav_recycler"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="15dp"/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</FrameLayout>
和
MainAdapter.java:Recycler 视图的适配器,还处理使项目(在本例中为 post)被添加的代码或从 firestore 中删除
public class MainAdapter extends FirestoreRecyclerAdapter<Posts, MainAdapter.ViewHolder> {
/**
* Create a new RecyclerView adapter that listens to a Firestore Query. See {@link
* FirestoreRecyclerOptions} for configuration options.
*
* @param options
*/
public MainAdapter(@NonNull FirestoreRecyclerOptions options) {
super(options);
}
private FirebaseFirestore db;
private DocumentReference documentReference;
private FirebaseAuth mAuth;
boolean isthere = false;
boolean isExist = false;
@Override
protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull Posts post) {
db = FirebaseFirestore.getInstance();
mAuth = FirebaseAuth.getInstance();
documentReference = db.collection("users").document(mAuth.getUid());
holder.txtTitle.setText(post.getName());
holder.txtDesc.setText(post.getTitle() + "\n" +post.getDesc() + "\n" + post.getBloodtype() + "\n" + post.getCity() + "\n" + post.getNumber()+ "\n" + post.getDeadline());
Glide.with(holder.image.getContext()).load(post.getImage())
.into(holder.image);
holder.root.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Snackbar.make(v, post.getUserid() + "\n" + post.getName() + "\n" +post.getBloodtype() + "\n" + post.getCity(), Snackbar.LENGTH_LONG)
.setAnchorView(R.id.navigation) // Set SnackBar above the BottomNavigationView
.show();
}
});
Posts newPost = new Posts(post.getUserid(), post.getName(), post.getTitle(),post.getDesc(), post.getDeadline(), post.getNumber(),
post.getCity(), post.getBloodtype(), post.getImage(), post.getPosttime(), post.getPostid());
holder.fav.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
// Toast.makeText(buttonView.getContext(), "Added to favorite", Toast.LENGTH_SHORT).show();
if (isExist == false) {
db.collection("users").document(mAuth.getUid()).collection("favorites").document(post.getPostid())
.set(newPost)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(buttonView.getContext(), "Succeed \nBoolean: " + isExist, Toast.LENGTH_SHORT).show();
}
});
}
}
else {
// removefromfav(post.getPostid(), buttonView);
documentReference.collection("favorites").document(post.getPostid())
.delete()
.addOnSuccessListener(new OnSuccessListener<Void>() {
public void onSuccess(Void aVoid) {
// Toast.makeText(buttonView.getContext(), "Removed from favorite " + isExist, Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
}
}
});
documentReference.collection("favorites").document(post.getPostid())
.get()
.addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if(task.isSuccessful()){
isExist = task.getResult().exists();
if (isExist == true) {
holder.fav.setChecked(true);
}
}
}
});
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.users_item, parent, false);
return new ViewHolder(view);
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public LinearLayout root;
public TextView txtTitle;
public TextView txtDesc;
public ImageView image;
public ToggleButton fav;
public ViewHolder(View itemView) {
super(itemView);
root = itemView.findViewById(R.id.list_root);
txtTitle = (TextView) itemView.findViewById(R.id.list_title);
txtDesc = (TextView) itemView.findViewById(R.id.list_desc);
image = (ImageView)itemView.findViewById(R.id.list_image);
fav = (ToggleButton)itemView.findViewById(R.id.favbutton);
}
}
}
编辑:
我添加了帖子class
Posts.java:
public class Posts {
private String userid;
private String name;
private String title;
private String desc;
private String deadline;
private String number;
private String city;
private String bloodtype;
private String image;
private String posttime;
private String postid;
public Posts(){
}
public Posts(String userid,String name, String title, String desc, String deadline,
String number, String city, String bloodtype, String image, String posttime, String postid){
this.userid = userid;
this.name = name;
this.title = title;
this.desc = desc;
this.deadline = deadline;
this.number = number;
this.city = city;
this.bloodtype = bloodtype;
this.image = image;
this.posttime = posttime;
this.postid = postid;
}
public String getUserid(){
return userid;
}
public String getName(){
return name;
}
public String getTitle(){
return title;
}
public String getDesc(){
return desc;
}
public String getDeadline(){
return deadline;
}
public String getNumber(){
return number;
}
public String getCity(){
return city;
}
public String getBloodtype(){
return bloodtype;
}
public String getImage(){
return image;
}
public String getPosttime(){
return posttime;
}
public String getPostid(){
return postid;
}
}
下面是 posts 在 firestore 中的截图:
好吧,我明白了,我错过了 onStart 和 onStop 来启动和停止侦听适配器。
@Override
public void onStart() {
super.onStart();
adapter.startListening();
}
@Override
public void onStop() {
super.onStop();
adapter.stopListening();
}