简体   繁体   English

如何将recyclerview中的所有数据传递给另一个活动并在listview或recyclerview中显示它 Firebase

[英]How can I pass all the data in the recyclerview to another activity and show it in the listview or recyclerview Firebase

In an activity, I fetch the data from the firebase and print it to the RecyclerView .在一个活动中,我从 firebase 获取数据并将其打印到RecyclerView I want to show all the data in the RecyclerView in another activity again in the RecyclerView .我想在RecyclerView的另一个活动中再次显示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.由于2个活动有相同的数据,我不想在2.活动中连接firebase并再次拉取数据。

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:评论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.当我按下按钮时,我希望它将 recyclerview 中的所有数据发送到其他活动。 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: RecyclerView其他活动的信息:


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

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 othars_activity:接收和设置所有数据

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

     

more simplified: Main_activity:更简化: 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 othars_activity:接收和设置所有数据

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM