我怎样才能查看帖子,只能从朋友那里查看?
在这个例子中,我想在RecyclerView中只显示我朋友的帖子。在这种情况下,用户15(当然是几个朋友,我只举一个例子)
结构 - 数据库:
USERS
-user01 (current user)
-name: John
-age: 20
--friends
----user10
----user15
-POSTS
--idpost_98685754
---user_post: user15
---title_post: Title
---photo: link
-posts
--idpost_41254d4554s1
---user_post: user40
---title_post: Title
---photo: link
-posts
--idpost_121221sss1
---user_post: user15
---title_post: Title 2
---photo: link 2
-posts
--idpost_9865565555
---user_post: user15
---title_post: Title 3
---photo: link 3
我目前只使用documentation中解释的简单顺序和限制功能。这是我第一次需要这样做。
编辑:
CODE - FRAGMENT:
/*Firebase*/
mDb = FirebaseFirestore.getInstance();
mQuery = mDb.collection("Posts");
Query mQueryNew = mQuery.orderBy("timestamp", Query.Direction.DESCENDING);
/* Recycler */
mCardFeedList = (RecyclerView) view.findViewById(id.cardFeedUser_list);
mCardFeedList.setHasFixedSize(true);
mCardFeedList.setItemViewCacheSize(20);
mCardFeedList.setDrawingCacheEnabled(true);
mAdapter = new PostsAdapter(mQueryNew, this){
@Override
protected void onDataChanged() {
if (getItemCount() == 0) {
mCardFeedList.setVisibility(View.GONE);
} else {
mCardFeedList.setVisibility(View.VISIBLE);
}
}
};
llmanager = new LinearLayoutManager(getActivity());
mCardFeedList.setLayoutManager(llmanager);
mCardFeedList.setAdapter(mAdapter);
/*START*/
if (mAdapter != null) {
mAdapter.startListening();
}
ADAPTER:
public class PostsAdapter extends FirestoreAdapter<PostsAdapter.ViewHolder> {
public interface OnPostsListner{
void onPostSelected(DocumentSnapshot post);
void onLikeSelected(DocumentSnapshot like);
void onCurtidasSelected(DocumentSnapshot curtidas);
void onFotoSelected(DocumentSnapshot foto);
void onCommentsSelected(DocumentSnapshot coments);
}
private OnPostsListner mListner;
public PostsAdapter (Query query, OnPostsListner listner){
super(query);
mListner = listner;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
return new ViewHolder(inflater.inflate(R.layout.card_feed_user_row, parent, false));
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.bind(getSnapshot(position), mListner);
}
static class ViewHolder extends RecyclerView.ViewHolder {
private Boolean mIsliked = false;
@BindView(R.id.tvCardFeedUser_nome)
TextView nomeUser;
@BindView(R.id.tvCardFeedUser_horario)
TextView horaFeed;
@BindView(R.id.tvCardFeedUser_desc)
TextView descFeed;
@BindView(R.id.ivCardFeedUser_image)
ImageView imageFeed;
@BindView(R.id.secaoLike)
LinearLayout curtir;
@BindView(R.id.secaoComments)
LinearLayout comentarios;
@BindView(R.id.tv_card_feed_linear_curtidas)
LinearLayout linear_curtidas;
@BindView(R.id.idBtnLike)
TextView btnLike;
@BindView(R.id.tv_card_feed_numero_curtidas)
TextView curtidas;
@BindView(R.id.tv_card_feed_nome_curtida)
TextView nome_curtidas;
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
public void bind(final DocumentSnapshot snapshot,
final OnPostsListner listener) {
final Posts ref = snapshot.toObject(Posts.class);
FirebaseFirestore db = FirebaseFirestore.getInstance();
FirebaseAuth mAuth = FirebaseAuth.getInstance();
final FirebaseUser mCurrentUser = mAuth.getCurrentUser();
nomeUser.setText(ref.getNome_user());
descFeed.setText(ref.getDesc());
//Foto
Picasso.with(itemView.getContext()).load(ref.getImage()).placeholder(R.drawable.bck_padrao).networkPolicy(NetworkPolicy.OFFLINE).into(imageFeed, new Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError() {
Picasso.with(itemView.getContext()).load(ref.getImage()).placeholder(R.drawable.bck_padrao).into(imageFeed);
}
});
SimpleDateFormat formatter = new SimpleDateFormat("dd/M/yy HH:mm");
String novaData = formatter.format(ref.getTimestamp());
horaFeed.setText(novaData);
/*CLICKS*/
curtir.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (listener != null) {
listener.onLikeSelected(snapshot);
}
}
});
imageFeed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (listener != null) {
listener.onFotoSelected(snapshot);
}
}
});
comentarios.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (listener != null) {
listener.onCommentsSelected(snapshot);
}
}
});
linear_curtidas.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (listener != null) {
listener.onCurtidasSelected(snapshot);
}
}
});
// Click listener
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (listener != null) {
listener.onPostSelected(snapshot);
}
}
});
}
}
}
答案 0 :(得分:0)
要实现此目的,您需要使用嵌套查询两次查询数据库。首先,您需要查询数据库以获取与单个用户对应的所有朋友ID,然后根据这些ID来第二次查询数据库,以仅显示与这些特定朋友对应的帖子。
需要注意的是,这不能在单个查询中完成。