Home > Software design >  How can I pass all the data in the recyclerview to another activity and show it in the listview or r
How can I pass all the data in the recyclerview to another activity and show it in the listview or r

Time:06-13

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);
            }
        });

RecyclerViewinformation 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);

CodePudding user response:

Main_activity:

///>>>
    public static String p_username= "username";
    public static String p_date= "date";
    public static String p_comment= "comment";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.c_home);

//////.....................................
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());
//>>>
p_username= model.getUsername();
p_date= model.getDate());
p_comment= model.getComment());
//............................................

postfab button:

postfab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent ıntent = new Intent(context,CommentsActivity.class);

                        intent.putExtra("username", p_username);
                        intent.putExtra("date", p_username);
                        intent.putExtra("comment", p_username);
                      
                startActivity(ıntent);
            }
        });

//.....OR......>>>

postfab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent ıntent = new Intent(context,CommentsActivity.class);

                        intent.putExtra(p_username , p_username );
                        intent.putExtra(p_date , p_date );
                        intent.putExtra(p_comment, p_comment);
                      
                startActivity(ıntent);
            }
        });

othars_activity: receive & set all the data

package com.your.package;

import static com.your.package.Main_activity.p_username ;
import static com.your.package.Main_activity.p_date ;
import static com.your.package.Main_activity.p_comment ;
//................................//////

String s_username = p_username;
 String s_date = p_date ;
  String s_comment = p_comment ;
//..............................
            TextView username = v.findViewById(R.id.comment_username);
               TextView date = v.findViewById(R.id.comment_date);
               TextView comment = v.findViewById(R.id.comment_text);
//................................
 set all the data
//...
            username .setText(s_username );
            date .setText(s_date );
             comment.setText(s_comment );

     

CodePudding user response:

more simplified: Main_activity:

///>>>
    private String p_username;
    private String p_date;
    private String p_comment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.c_home);

//////.....................................
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());
//>>>
p_username= model.getUsername();
p_date= model.getDate());
p_comment= model.getComment());
//............................................

postfab button:

postfab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent ıntent = new Intent(context,CommentsActivity.class);

                        intent.putExtra("username", p_username);
                        intent.putExtra("date", p_username);
                        intent.putExtra("comment", p_username);
                      
                startActivity(ıntent);
            }
        });

othars_activity: receive & set all the data

String s_username = getIntent().getStringExtra("username");
 String s_date = getIntent().getStringExtra("date");
  String s_comment = getIntent().getStringExtra("comment");
//..............................
                TextView username = v.findViewById(R.id.comment_username);
               TextView date = v.findViewById(R.id.comment_date);
               TextView comment = v.findViewById(R.id.comment_text);
//................................
 set all the data
//...
                username .setText(s_username );
                date .setText(s_date );
                 comment.setText(s_comment );
 
  • Related