Home Ask Login Register

Developers Planet

Your answer is one click away!

Im Rick James February 2016

Pass Json Object from ViewHolder to Fragment

I have a RecyclerView and a Fragment. I want to pass a JSONObject from RecyclerView to Fragment. So, I created an Interface and implemented it on Fragment and on RecyclerView. I initialized the variable and access the method in the fragment passing the JSONObject to it, however I am getting a NPE when trying to access the method:

public class ProductResultsListFragment extends  Fragment implements ProductResultAdapterInterface{
//code here

public void showResultsInMap(JSONObject mapObject) 

In my RV class I have the following:

public class ProductSearchAdapter extends RecyclerView.Adapter<ProductSearchAdapter.ViewHolder>  {

public ProductResultAdapterInterface mProductResultsListener;

if (mapObjects.length()>0)

The if statement is inside my viewHolder in my RecyclerView class but the instance is made public.

I have tried casting my mProductResultsListener but dont know what class to cast it into.

public ProductResultAdapterInterface mProductResultsListener=((ProductResultAdapterInterface ) ?????);

Just a quick comment: the mapObject (JsonObject) is created on a onClick method inside a button in ViewHolder, I cannot pass bundle on OnCreate method.

 this.btnMarkItemMap.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                int position  =   getAdapterPosition();

                switch (v.getId())
                    case (R.id.markProductinMap):

                        String mapObj=null;
                        if (mapObjects !=nu        


Mo1989 February 2016

where have you assigned the variable mProductResultsListener? you need to assign this to the object of the fragment by passing it into the adapter. Otherwise its value is null and you will get a NullPointeException if you call a function on a null object.

If you have, then please update the question with that code.

Aziz February 2016

You can pass the data using bundles like this

SomeFragment someFragment = new SomeFragment();
    Bundle bundle = new Bundle();
    bundle.putString("value1", "2");
    bundle.putString("value2", "23");
    bundle.putString("value3", "276");
    bundle.putString("value4", "27");
    bundle.putBoolean("flag", true);

And in SomeFragment

Bundle bundle = getArguments();

    strValue1 = bundle.getString("value1");
    strValue2 = bundle.getString("value2");

and so on for getting all the passed data from bundle

Fabio Venturi Pastor February 2016

ProductResultsListFragment yourFragment = new ProductResultsListFragment();
Bundle bundle = new Bundle();
bundle.putString("yourJsonObject", yourJsonObject.toString);

public class ProductResultsListFragment...
    public void onCreate(Bundle savedInstanceState) {
        Bundle savedInfo = this.getArguments();
        String savedJsonString = savedInfo.getString("yourJsonObject");
        JSONObject myJsonObject = new JSONObject(savedJsonString);

Im Rick James February 2016

On my Fragment adapter init, I added:

ProductResultAdapterInterface mProInterface=(ProductResultAdapterInterface) this;
        mAdapter = new ProductSearchAdapter(R.layout.product_results_cardlist,getActivity(),productListFeed,mProInterface);

on my adapter constructor I changed the signature to accept the Interface as a parameter passed from the Fragment

 public ProductSearchAdapter (int rowLayout, Context context,List<Product> feedList,ProductResultAdapterInterface mProductResultsListener) {

        this.rowLayout = rowLayout;
        this.mContext = context;


Then I was able to access the method and not getting NPE. thanks all

Post Status

Asked in February 2016
Viewed 1,535 times
Voted 4
Answered 4 times


Leave an answer

Quote of the day: live life