Skip to content Skip to sidebar Skip to footer

How To Pass Or Send Data From Recyclerview Adapter To Fragment

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 unfortunatel

Solution 1:

You can pass data by attaching a callback into your adapter:

Create an interface:

interfaceOnTextClickListener {
    voidonTextClick(ExampleItem data);
}

Delegate the callback:

classExampleAdapterextendsRecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {

    List<ExampleItem> items;
    OnTextClickListener listener;

    ExampleAdapter(List<ExampleItem> items, OnTextClickListener listener) {
        this.items = items;
        this.listener = listener;
    }

    @OverridepublicvoidonBindViewHolder(ExampleViewHolder viewHolder, int position) {
        viewHolder.textView.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View v) {
                // Say you want to pass an ExampleItem back to the fragment...ExampleItemdata= items.get(position);
                listener.onTextClick(data);
            }
        });
    }
}

Implement the fragment with the interface:

classYourFragmentextendsFragmentimplementsOnTextClickListener {

    publiconViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView.setAdapter(newExampleAdapter(this));
    }

    voidonTextClick(ExampleItem data) {
        // Now you can do however you want with the data here...Toast.makeText(getActivity(), "Got: " + data, Toast.LENGTH_SHORT).show()
    }
}

Solution 2:

You have two options;

Using SharedPreferences:

ContextmyContext= exampleViewHolder.itemView.getContext()

SharedPreferences.Editoreditor= getSharedPreferences("DeviceToken", MODE_PRIVATE).edit();
editor.putString("YourKeyNameHere", currentItem.getmText()); // or add toString() after if needed
editor.apply();

To get:

SharedPreferencesprefs= getSharedPreferences("DeviceToken", MODE_PRIVATE);
StringdeviceToken= 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:

StringextraText= getIntent().getStringExtra("EXTRA_TEXT");

Solution 3:

You have "ClickListener" in fragment.

for example.

@OverridepublicvoidonResume(){
    super.onResume();
    ((ExampleAdapter) mAdapter).onItemClick(newRecyclerViewItemClick() {
        @OverridepublicvoidonItemClick(int position, View v) {
            //to do. ArrayList<> data.get(position).getYourItem();
        }
    });
}

And you must create interface RecyclerViewItemClick.java

publicinterfaceRecyclerViewItemClick {
    publicvoidonItemClick(int position, View v);
}

Last. ExampleAdapter.java add

publicvoidonItemClick(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.

Solution 4:

Have a IDataCollector interface. Implement her in the Fragment, pass the Fragment reference as a constructor parameter, and use it inside the adapter

publicinterface IDataCollector {
    voidsetText(String text);
    voidsetAnything(Object anything);
}

// FragmentpublicclassFragmentimplements IDataCollector {

    privatevoidinitView() {
        // Add this as a parameterExampleAdapter adapter = newExampleAdapter(... , this);
    }

    @OverridepublicvoidsetText(String text){
        this.textField = text;
    }

    @OverridepublicvoidsetText(Object anything){
        this.anything = anything;
    }
}

// Adapter code// Constructor, parameters ommitedpublicExampleAdapter(...., IDataCollector dataCollector) {
    this.dataCollector = dataCollector;
}

@OverridepublicvoidonBindViewHolder(@NonNull ExampleViewHolder exampleViewHolder, int i) {
    final ExampleItem currentItem=mExampleList.get(i);
    exampleViewHolder.mTextView.setText(currentItem.getmText());
    exampleViewHolder.mTextView.setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View v) {
            dataCollector.setText(((TextView)v.findViewById(R.id.label1)).getText());
            ....
        }
    });
}

Solution 5:

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

Post a Comment for "How To Pass Or Send Data From Recyclerview Adapter To Fragment"