Saket
Saket

Reputation: 79

How to send JSON data from Activity to Fragment?

I am trying to fetch data from JSON and send a part of data to all Fragments. Here is my code:

    private void setupViewPager(ViewPager viewPager,ArrayList<Product> productname,int p) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        Bundle args = new Bundle();
 }
        String price = productname.get(1).getPrice();
        args.putString("key", price);
        TheFragment tf1 = new TheFragment();
        TheFragment tf2 = new TheFragment();
        TheFragment tf3 = new TheFragment();
        TheFragment tf4 = new TheFragment();
        adapter.addFragment(tf1 , "ONE");
        tf1.setArguments(args);
        adapter.addFragment(tf2 , "Two");
        tf2.setArguments(args);
        adapter.addFragment(tf3 , "Three");
        tf3.setArguments(args);
        adapter.addFragment(tf4 , "Four");
        tf4.setArguments(args); 

 //3      adapter.addFragment(new TheFragment(), "TWO"); // Can I send data by coding this way?
 //3     adapter.addFragment(new TheFragment(), "THREE");
 //3       adapter.addFragment(new TheFragment(),"THE 4");
       viewPager.setAdapter(adapter);
    }

Before this I have retrieved JSON value in a array list and n=jsonArray.length();.

setupViewPager(viewPager,arrayList,n);

The JSON file contains name, price and image. I want to display prices in my fragments.

public class TheFragment extends Fragment {

    TextView tv;
    public TheFragment() {

    }
    String value = getArguments().getString("key");

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    /*    if(value!=null){
            tv.setText(value);
        } */

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_this, container, false);
        tv = (TextView) getView().findViewById(R.id.thePrice);
        tv.setText(value);
        return view;
    }
}

The code is not showing any error but app is not running. What are the mistakes I am doing and possible solutions available? Thanks.

Upvotes: 3

Views: 1284

Answers (2)

SriMaharshi Manchem
SriMaharshi Manchem

Reputation: 534

Hi Use newInstance method in fragment as below.

public static Fragment newInstance(ArrayList<Product> jsonData) {

            Bundle args = new Bundle();
            args.putStringArrayList("Key","jsonDatainArray");
            fragment.setArguments(args);
            return fragment;
}

And initialize fragment as below

 TheFragment theFragment= TheFragment.newInstance("arrayListHere");

Use getArguments() method to get the arrayList in onCreateView or onCreate in Fragment.

Upvotes: 0

Barno
Barno

Reputation: 786

Change this lines of code in your fragment-

   public class TheFragment extends Fragment {

    TextView tv;
    public TheFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_this, container, false);
    }

   @Override
   public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
       super.onViewCreated(view, savedInstanceState);
       tv = (TextView) view.findViewById(R.id.thePrice);
   }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        String price = getArguments().getString("key");
        tv.setText(price);
    }
}

Hope will work well.

Upvotes: 1

Related Questions