簡體   English   中英

如何將JSON數據從Activity發送到Fragment?

[英]How to send JSON data from Activity to Fragment?

我正在嘗試從JSON提取數據並將部分數據發送到所有Fragments。 這是我的代碼:

    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);
    }

在此之前,我已經在數組列表和n=jsonArray.length();檢索了JSON值n=jsonArray.length();

setupViewPager(viewPager,arrayList,n);

JSON文件包含名稱,價格和圖像。 我想在片段中顯示價格。

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;
    }
}

代碼未顯示任何錯誤,但應用程序未運行。 我在犯什么錯誤以及可能的解決方案? 謝謝。

在您的片段中更改此行代碼-

   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);
    }
}

希望會運作良好。

嗨,在下面的片段中使用newInstance方法。

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

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

並如下初始化片段

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

使用getArguments()方法獲取onCreate中的arrayList或Fragment中的onCreateView

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM