In an activity, I fetch the data from the firebase and print it to the RecyclerView
. I want to show all the data in the RecyclerView
in another activity again in the RecyclerView
. How can I do that? Since 2 activities have the same data, I don't want to connect firebase in 2.activity and pull data again.
recyclerviewcomment = findViewById(R.id.recyclerviewcomment);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
recyclerviewcomment.setLayoutManager(linearLayoutManager);
getcomments();
private void getcomments(){
FirebaseRecyclerOptions <Comments> options =
new FirebaseRecyclerOptions.Builder<Comments>()
.setQuery(CommentsRef.orderByChild("timestamp"),Comments.class)
.build();
FirebaseRecyclerAdapter<Comments,CommentsViewHolder> adapter
=new FirebaseRecyclerAdapter<Comments, CommentsViewHolder>(options)
{
@Override
protected void onBindViewHolder(@NonNull final PostActivity.CommentsViewHolder holder, final int position, @NonNull final Comments model) {
Random ran=new Random();
int i=ran.nextInt(photos.length);
holder.comment_profil.setImageResource(photos[i]);
holder.comment_username.setText(model.getUsername());
holder.comment_date.setText(model.getDate());
try {
commentwords = model.getComment();
for (String word : words) {
Pattern rx = Pattern.compile("\\b" + word + "\\b", Pattern.CASE_INSENSITIVE);
commentwords = rx.matcher(commentwords).replaceAll(new String(new char[word.length()]).replace('\0', '*'));
}
holder.comment_text.setText(commentwords);
}catch (Exception e){
}
}
@NonNull
@Override
public PostActivity.CommentsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.like_comments_model,viewGroup,false);
PostActivity.CommentsViewHolder viewHolder = new PostActivity.CommentsViewHolder(view);
return viewHolder;
}
};
adapter.startListening();
recyclerviewcomment.setAdapter(adapter);
}
public static class CommentsViewHolder extends RecyclerView.ViewHolder{
TextView comment_username,comment_date,comment_text , commentsanswer;
ImageView comment_profil;
View mView ;
public CommentsViewHolder(@NonNull View itemView) {
super(itemView);
mView=itemView;
comment_username = itemView.findViewById(R.id.comment_username);
comment_date = itemView.findViewById(R.id.comment_date);
comment_text = itemView.findViewById(R.id.comment_text);
comment_profil = itemView.findViewById(R.id.comment_profil);
commentsanswer = itemView.findViewById(R.id.commentsanswer);
}
}
Comments Model:
public class Comments {
public String comment , timestamp , username , date , time , currentUserID ;
public Comments(){
}
public Comments(String comment, String timestamp, String username , String date , String time , String currentUserID) {
this.comment = comment;
this.username = username;
this.timestamp = timestamp;
this.date = date;
this.time = time;
this.currentUserID = currentUserID;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getCurrentUserID() {
return currentUserID;
}
public void setCurrentUserID(String currentUserID) {
this.currentUserID = currentUserID;
}
}
When I press the button, I want it to send all the data in the recyclerview to the other activiyt. I just want to send username,date and comments information:
postfab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent ıntent = new Intent(context,CommentsActivity.class);
startActivity(ıntent);
}
});
RecyclerView
information for other activity :
R.layout.all_comments_model
TextView username = v.findViewById(R.id.comment_username);
TextView date = v.findViewById(R.id.comment_date);
TextView comment = v.findViewById(R.id.comment_text);