How to pass or send data from recyclerview adapter to fragment

前端 未结 5 1428
执念已碎
执念已碎 2021-01-16 12:20

How to pass data from recyclerview adapter to fragment and what i have to write the code in fragment to receive data.I have already check the links in this website but unfor

5条回答
  •  长情又很酷
    2021-01-16 12:48

    You have two options;

    Using SharedPreferences:

    Context myContext = exampleViewHolder.itemView.getContext()
    
    SharedPreferences.Editor editor = getSharedPreferences("DeviceToken", MODE_PRIVATE).edit();
    editor.putString("YourKeyNameHere", currentItem.getmText()); // or add toString() after if needed
    editor.apply();
    

    To get:

    SharedPreferences prefs = getSharedPreferences("DeviceToken", MODE_PRIVATE);
    String deviceToken = prefs.getString("YourKeyNameHere", null); // get it here
    

    Using Intent: (If you want to start the another Activity when you pass the data)

    Intent intent = new Intent(myContext , SignoutActivity.class);
    intent.putExtra("EXTRA_TEXT", currentItem.getmText());
    startActivity(intent); // if needed, add myContext before starting myContext.startActivity...
    

    To get:

    String extraText = getIntent().getStringExtra("EXTRA_TEXT");
    

提交回复
热议问题