我想显示按数据库值(numLike)排序的帖子。
我希望只有收到超过10条建议的帖子 将被打印。它运行正常,没有错误。
但是值小于10的帖子也被打印。
我已经两天没有解决这个问题了……
final ArrayList<Integer> arrayList = new ArrayList<>();DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference("posts");
dbRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
arrayList.clear();
for (DataSnapshot ds: dataSnapshot.getChildren()) {
int numLike = ds.child("numLikes").getValue(Integer.class);
arrayList.add(numLike);
int i = 10;
if(numLike >= i){
init(); //Function to place a posts
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
init()
private void init() {
mPostRecyclerView = (RecyclerView) mRootVIew.findViewById(R.id.recyclerview_post2);
mPostRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
setupAdapter();
mPostRecyclerView.setAdapter(mPostAdapter);
//reverse
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getContext());
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
mPostRecyclerView.setLayoutManager(mLayoutManager);
}
setupAdapter()
private void setupAdapter() {
mPostAdapter = new FirebaseRecyclerAdapter<Post, HomeFragment.PostHolder>(
Post.class,
R.layout.row_post,
HomeFragment.PostHolder.class,
FirebaseUtils.getPostRef()
) {
@Override
public void populateViewHolder(HomeFragment.PostHolder viewHolder, final Post model, int position) {
viewHolder.setNumCOmments(String.valueOf(model.getNumComments()));
viewHolder.setNumLikes(String.valueOf(model.getNumLikes()));
viewHolder.setTIme(DateUtils.getRelativeTimeSpanString(model.getTimeCreated()));
viewHolder.setUsername(model.getUser().getUser());
viewHolder.setPostText(model.getPostText());
Glide.with(getActivity())
.load(model.getUser().getPhotoUrl())
.into(viewHolder.postOwnerDisplayImageView);
if (model.getPostImageUrl() != null) {
viewHolder.postDisplayImageVIew.setVisibility(View.VISIBLE);
StorageReference storageReference = FirebaseStorage.getInstance()
.getReference(model.getPostImageUrl());
Glide.with(getActivity())
.using(new FirebaseImageLoader())
.load(storageReference)
.into(viewHolder.postDisplayImageVIew);
} else {
viewHolder.postDisplayImageVIew.setImageBitmap(null);
viewHolder.postDisplayImageVIew.setVisibility(View.GONE);
}
viewHolder.postLikeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onLikeClick(model.getPostId());
}
});
viewHolder.postCommentLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), PostActivity.class);
intent.putExtra(Constants.EXTRA_POST, model);
startActivity(intent);
}
});
}
};
}
我要复制应用程序“ 9GAG” ..