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
You have "ClickListener" in fragment.
for example.
@Override
public void onResume(){
super.onResume();
((ExampleAdapter) mAdapter).onItemClick(new RecyclerViewItemClick() {
@Override
public void onItemClick(int position, View v) {
//to do. ArrayList<> data.get(position).getYourItem();
}
});
}
And you must create interface RecyclerViewItemClick.java
public interface RecyclerViewItemClick {
public void onItemClick(int position, View v);
}
Last. ExampleAdapter.java add
public void onItemClick(RecyclerViewItemClick mclick){
this.myClickListener = mclick;
}
ExampleViewHolder method add in
@Override
public void onClick(View v) {
myClickListener.onItemClick(getAdapterPosition(), v);
notifyDataSetChanged();
}
I hope you solve this problem. Thank.
Have a IDataCollector
interface. Implement her in the Fragment
, pass the Fragment
reference as a constructor parameter, and use it inside the adapter
public interface IDataCollector {
void setText(String text);
void setAnything(Object anything);
}
// Fragment
public class Fragment implements IDataCollector {
private void initView() {
// Add this as a parameter
ExampleAdapter adapter = new ExampleAdapter(... , this);
}
@Override
public void setText(String text){
this.textField = text;
}
@Override
public void setText(Object anything){
this.anything = anything;
}
}
// Adapter code
// Constructor, parameters ommited
public ExampleAdapter(...., IDataCollector dataCollector) {
this.dataCollector = dataCollector;
}
@Override
public void onBindViewHolder(@NonNull ExampleViewHolder exampleViewHolder, int i) {
final ExampleItem currentItem=mExampleList.get(i);
exampleViewHolder.mTextView.setText(currentItem.getmText());
exampleViewHolder.mTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dataCollector.setText(((TextView)v.findViewById(R.id.label1)).getText());
....
}
});
}
You can pass data by attaching a callback into your adapter:
Create an interface:
interface OnTextClickListener {
void onTextClick(ExampleItem data);
}
Delegate the callback:
class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
List<ExampleItem> items;
OnTextClickListener listener;
ExampleAdapter(List<ExampleItem> items, OnTextClickListener listener) {
this.items = items;
this.listener = listener;
}
@Override
public void onBindViewHolder(ExampleViewHolder viewHolder, int position) {
viewHolder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Say you want to pass an ExampleItem back to the fragment...
ExampleItem data = items.get(position);
listener.onTextClick(data);
}
});
}
}
Implement the fragment with the interface:
class YourFragment extends Fragment implements OnTextClickListener {
public onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView.setAdapter(new ExampleAdapter(this));
}
void onTextClick(ExampleItem data) {
// Now you can do however you want with the data here...
Toast.makeText(getActivity(), "Got: " + data, Toast.LENGTH_SHORT).show()
}
}
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");
Through Interface you can solve this 1. create an Interface in adapter , add required methods 2. call those methods in adapter wherever required 3. implement the interface in fragment or activity, pass a context to the adapter