简体   繁体   English

如何在适配器中使用 putExtra 和 getExtra 意图?

[英]How to use putExtra and getExtra intent in adapter?

I wanted to pass some data to adapter to java class.我想将一些数据传递给适配器到 java class。 My adapter send data but java class show null check my code and help me if write something wrong !!!我的适配器发送数据,但 java class 显示 null 检查我的代码,如果写错了帮助我!!!

here is my adapter class这是我的适配器 class

 public class myadapter extends RecyclerView.Adapter<myadapter.ViewHolder> {
        private List<Chat> values;
        private Context context;
        public static final int MSG_TYPE_LEFT=0;
        public static final int MSG_TYPE_RIGHT=1;
        String text;
        FirebaseUser fuser;
        DatabaseReference reference,reference1;
        public myadapter(List<Chat> mchat,Context context) {
            this.values =mchat  ;
            this.context=context;
        }
    
    public void onBindViewHolder(@NonNull @NotNull myadapter.ViewHolder holder, int position) {
            Chat name = values.get(position);
    
            Resources res = holder.itemView.getContext().getResources();
            String cur=name.getMsgid();
            Chat prev=values.get(position-1);
            String pr=prev.getMsgid();
            Intent pre=new Intent(context,chatscreen.class);
            pre.putExtra("pmsgid",prev.getMsgid());//i want this data to my java class
            context.startActivity(pre);
    }

here is my chatscreen.java
public class chatscreen extends AppCompatActivity {
    RecyclerView rv2;
    RecyclerView.Adapter mAdapter;
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chatscreen);
      Intent pre=getIntent();
            prevmsgid=pre.getStringExtra("pmsgid");
    mAdapter =new myadapter(mchat,getApplicationContext());
                    rv2.setAdapter(mAdapter);
                    mAdapter.notifyDataSetChanged();

 mAdapter =new myadapter(mchat,getApplicationContext());
                rv2.setAdapter(mAdapter);
                mAdapter.notifyDataSetChanged();

anyone knows how to get data?help me!有谁知道如何获取数据?帮帮我!

String pr=prev.getMsgid();
Intent pre=new Intent(context,chatscreen.class);
pre.putExtra("pmsgid",pr);
((Activity) context).startActivity(pre);

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

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