如何使用 FirebaseUI 库在 recyclerview 中显示特定用户的帖子 (Java Android)
How to show Posts by Specific users in a recyclerview using FirebaseUI library (Java Android)
我正在为我的社交应用程序使用 FirebaseUI 库。我能够将所有用户的帖子检索到回收站视图中。我的目标是提供另一个选项,用户可以在其中查看和编辑自己的帖子。我似乎无法从数据库中获取用户特定的帖子,我该怎么做呢?我也在 Fragment
中使用 recyclerview
数据库结构
后置适配器
public class PostsAdapter extends FirebaseRecyclerAdapter<Posts, PostsAdapter.postsViewholder> {
public PostsAdapter(@NonNull FirebaseRecyclerOptions<Posts> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull PostsAdapter.postsViewholder holder, int position, @NonNull Posts model) {
holder.name.setText(model.getName());
holder.location.setText(model.getLocation());
holder.occurence.setText(model.getOccurence());
final String userUid = model.getUid();
FirebaseDatabase.getInstance().getReference("Users").child(userUid).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()) {
final String pictureUrl = snapshot.child("url").getValue(String.class);
if (pictureUrl.isEmpty()) {
holder.img.setImageResource(R.drawable.ic_launcher_background);
} else {
Picasso.get().load(pictureUrl).into(holder.img);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
//String bro = "Admin";
if (holder.name.getText().toString() == "Admin") {
holder.verify.setVisibility(View.VISIBLE);
}
// holder.verify.setImageResource(R.drawable.star);
FirebaseDatabase rootNode = FirebaseDatabase.getInstance();
String keybro1 = getRef(position).getKey();
DatabaseReference reference = rootNode.getReference("UserPosts");
// email.setText(mAuth.getCurrentUser().getEmail());
holder.reply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), replyActivity.class);
String locationsn = (String) holder.location.getText();
String occurencesn = (String) holder.occurence.getText();
intent.putExtra("locationsn", locationsn);
intent.putExtra("occurencesn", occurencesn);
intent.putExtra("keybro", keybro1);
v.getContext().startActivity(intent);
}
});
holder.date.setText(model.getDate());
//Picasso.get().load(model.getUrl()).into(holder.img);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
@NonNull
@Override
public PostsAdapter.postsViewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.post, parent, false);
return new PostsAdapter.postsViewholder(view);
}
public class postsViewholder extends RecyclerView.ViewHolder {
TextView location, occurence, name, date, reply, key, share, report;
ImageView img, verify, imgshare;
public postsViewholder(@NonNull View itemView) {
super(itemView);
location = itemView.findViewById(R.id.incident_get);
occurence = itemView.findViewById(R.id.occurence_get);
name = itemView.findViewById(R.id.name_get_one);
reply = itemView.findViewById(R.id.reply);
share = itemView.findViewById(R.id.share_me);
report = itemView.findViewById(R.id.report_me);
imgshare = itemView.findViewById(R.id.imgshare);
verify = itemView.findViewById(R.id.verify);
img = itemView.findViewById(R.id.mainimg);
date = itemView.findViewById(R.id.date_time);
}
}
我如何检索所有帖子
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_posts, container, false);
RecyclerView recyclerView = view.findViewById(R.id.postsrecycler);
rootNode = FirebaseDatabase.getInstance();
mAuth = FirebaseAuth.getInstance();
change = view.findViewById(R.id.change);
FloatingActionButton floatingActionButton = view.findViewById(R.id.main_post);
mbase = FirebaseDatabase.getInstance().getReference("UserPosts");
// To display the Recycler view linearly
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
// It is a class provide by the FirebaseUI to make a
// query in the database to fetch appropriate data
FirebaseRecyclerOptions<Posts> options = new FirebaseRecyclerOptions.Builder<Posts>().setQuery(mbase, Posts.class).build();
// Connecting object of required Adapter class to
// the Adapter class itself
adapter = new PostsAdapter(options);
// Connecting Adapter class with the Recycler view*/
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getActivity(), postActivity.class));
}
});
return view;
}
@Override
public void onStart() {
super.onStart();
adapter.startListening();
}
@Override
public void onStop() {
super.onStop();
adapter.stopListening();
}}
预期结果截图
Home where users see all posts
Where users view their own posts
相关代码为:
mbase = FirebaseDatabase.getInstance().getReference("UserPosts");
// To display the Recycler view linearly
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
// It is a class provide by the FirebaseUI to make a
// query in the database to fetch appropriate data
FirebaseRecyclerOptions<Posts> options =
new FirebaseRecyclerOptions.Builder<Posts>()
.setQuery(mbase, Posts.class).build();
// Connecting object of required Adapter class to
// the Adapter class itself
adapter = new PostsAdapter(options);
由于您始终将 base
传递给适配器,因此它将始终显示 UserPosts
的所有子节点。要只显示特定用户的子节点,您可以 use a query:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
Query query = mbase.orderByChild("uid").equalTo(uid);
FirebaseRecyclerOptions<Posts> options =
new FirebaseRecyclerOptions.Builder<Posts>()
.setQuery(query, Posts.class).build();
我正在为我的社交应用程序使用 FirebaseUI 库。我能够将所有用户的帖子检索到回收站视图中。我的目标是提供另一个选项,用户可以在其中查看和编辑自己的帖子。我似乎无法从数据库中获取用户特定的帖子,我该怎么做呢?我也在 Fragment
中使用 recyclerview数据库结构
后置适配器
public class PostsAdapter extends FirebaseRecyclerAdapter<Posts, PostsAdapter.postsViewholder> {
public PostsAdapter(@NonNull FirebaseRecyclerOptions<Posts> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull PostsAdapter.postsViewholder holder, int position, @NonNull Posts model) {
holder.name.setText(model.getName());
holder.location.setText(model.getLocation());
holder.occurence.setText(model.getOccurence());
final String userUid = model.getUid();
FirebaseDatabase.getInstance().getReference("Users").child(userUid).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()) {
final String pictureUrl = snapshot.child("url").getValue(String.class);
if (pictureUrl.isEmpty()) {
holder.img.setImageResource(R.drawable.ic_launcher_background);
} else {
Picasso.get().load(pictureUrl).into(holder.img);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
//String bro = "Admin";
if (holder.name.getText().toString() == "Admin") {
holder.verify.setVisibility(View.VISIBLE);
}
// holder.verify.setImageResource(R.drawable.star);
FirebaseDatabase rootNode = FirebaseDatabase.getInstance();
String keybro1 = getRef(position).getKey();
DatabaseReference reference = rootNode.getReference("UserPosts");
// email.setText(mAuth.getCurrentUser().getEmail());
holder.reply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), replyActivity.class);
String locationsn = (String) holder.location.getText();
String occurencesn = (String) holder.occurence.getText();
intent.putExtra("locationsn", locationsn);
intent.putExtra("occurencesn", occurencesn);
intent.putExtra("keybro", keybro1);
v.getContext().startActivity(intent);
}
});
holder.date.setText(model.getDate());
//Picasso.get().load(model.getUrl()).into(holder.img);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
@NonNull
@Override
public PostsAdapter.postsViewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.post, parent, false);
return new PostsAdapter.postsViewholder(view);
}
public class postsViewholder extends RecyclerView.ViewHolder {
TextView location, occurence, name, date, reply, key, share, report;
ImageView img, verify, imgshare;
public postsViewholder(@NonNull View itemView) {
super(itemView);
location = itemView.findViewById(R.id.incident_get);
occurence = itemView.findViewById(R.id.occurence_get);
name = itemView.findViewById(R.id.name_get_one);
reply = itemView.findViewById(R.id.reply);
share = itemView.findViewById(R.id.share_me);
report = itemView.findViewById(R.id.report_me);
imgshare = itemView.findViewById(R.id.imgshare);
verify = itemView.findViewById(R.id.verify);
img = itemView.findViewById(R.id.mainimg);
date = itemView.findViewById(R.id.date_time);
}
}
我如何检索所有帖子
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_posts, container, false);
RecyclerView recyclerView = view.findViewById(R.id.postsrecycler);
rootNode = FirebaseDatabase.getInstance();
mAuth = FirebaseAuth.getInstance();
change = view.findViewById(R.id.change);
FloatingActionButton floatingActionButton = view.findViewById(R.id.main_post);
mbase = FirebaseDatabase.getInstance().getReference("UserPosts");
// To display the Recycler view linearly
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
// It is a class provide by the FirebaseUI to make a
// query in the database to fetch appropriate data
FirebaseRecyclerOptions<Posts> options = new FirebaseRecyclerOptions.Builder<Posts>().setQuery(mbase, Posts.class).build();
// Connecting object of required Adapter class to
// the Adapter class itself
adapter = new PostsAdapter(options);
// Connecting Adapter class with the Recycler view*/
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getActivity(), postActivity.class));
}
});
return view;
}
@Override
public void onStart() {
super.onStart();
adapter.startListening();
}
@Override
public void onStop() {
super.onStop();
adapter.stopListening();
}}
预期结果截图
Home where users see all posts
Where users view their own posts
相关代码为:
mbase = FirebaseDatabase.getInstance().getReference("UserPosts");
// To display the Recycler view linearly
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
// It is a class provide by the FirebaseUI to make a
// query in the database to fetch appropriate data
FirebaseRecyclerOptions<Posts> options =
new FirebaseRecyclerOptions.Builder<Posts>()
.setQuery(mbase, Posts.class).build();
// Connecting object of required Adapter class to
// the Adapter class itself
adapter = new PostsAdapter(options);
由于您始终将 base
传递给适配器,因此它将始终显示 UserPosts
的所有子节点。要只显示特定用户的子节点,您可以 use a query:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
Query query = mbase.orderByChild("uid").equalTo(uid);
FirebaseRecyclerOptions<Posts> options =
new FirebaseRecyclerOptions.Builder<Posts>()
.setQuery(query, Posts.class).build();