我一直在尝试获取Firebaserecycler适配器的itemcount。我的目的是检测recyclerview是否为空,并采取相应的措施。
private void loadFriendList2() {
Query query = FirebaseDatabase.getInstance()
.getReference(Commonx.USER_INFORMATION)
.child(Commonx.loggedUser.getUid())
.child(Commonx.ACCEPT_LIST);
FirebaseRecyclerOptions<User> options = new FirebaseRecyclerOptions.Builder<User>()
.setQuery(query, User.class)
.build();
adapter = new FirebaseRecyclerAdapter<User, AllFriendViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull final AllFriendViewHolder allFriendViewHolder, int i, @NonNull final User user) {
allFriendViewHolder.all_friends_txt_user_email.setText(new StringBuilder(user.getEmail()));
DatabaseReference referencex = FirebaseDatabase.getInstance().getReference().child(Commonx.USER_INFORMATION);
Query newx = referencex.orderByChild("uid").equalTo(user.getUid());
newx.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
childs = getSnapshots().size();
Toast.makeText(HomeActivity.this, String.valueOf(childs), Toast.LENGTH_SHORT).show(); // it do prints the value of child if exits
if (dataSnapshot.exists()) {
for (DataSnapshot userx : dataSnapshot.getChildren()) {
if (userx.child("image").exists()) {
Picasso.get().load(userx.child("image").getValue().toString()).into(allFriendViewHolder.all_friends_profile_image);
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
allFriendViewHolder.setiRecycItemListerner(new IRecycItemListerner() {
@Override
public void onItemClickListener(View view, int position) {
//show tracking information
Commonx.trackingUser = user;
startActivity(new Intent(HomeActivity.this, TrackingActivity.class));
}
});
allFriendViewHolder.all_friends_locate_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Commonx.userProfile = user;
startActivity(new Intent(HomeActivity.this, UserProfileActivity.class));
}
});
}
@NonNull
@Override
public AllFriendViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout_all_friends, parent, false);
return new AllFriendViewHolder(itemView);
}
@NonNull
@Override
public User getItem(int position) {
return super.getItem(position);
}
};
adapter.startListening();
recycler_friend_list.setAdapter(adapter);
if (adapter.getItemCount() == 0) {
Toast.makeText(this, "No users in the friend list", Toast.LENGTH_SHORT).show();
}
}
以下代码确实打印child(假设1或2)的值。但是,如果我将其打印在适配器的花括号之外,它将始终将itemcount打印为0。
childs = getSnapshots().size();
Toast.makeText(HomeActivity.this, String.valueOf(childs), Toast.LENGTH_SHORT).show();
而且,尽管RecyclerView的itemcout填充了项目,但以下代码始终为0。
if (adapter.getItemCount() == 0) {
Toast.makeText(this, "No users in the friend list", Toast.LENGTH_SHORT).show();
Toast.makeText(this, String.valueOf(adapter.getItemCount()), Toast.LENGTH_SHORT).show();
}
而且,我已经将以下方法称为getItemCount()。
@NonNull
@Override
public User getItem(int position) {
return super.getItem(position);
}