如何将JSON数据从活动发送到片段?

问题描述:

我想从JSON中获取数据并将一部分数据发送到所有碎片。 这里是我的代码:如何将JSON数据从活动发送到片段?

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值之前。

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

该代码未显示任何错误,但应用程序未运行。我正在做什么错误和可能的解决方案?谢谢。

+0

是您的片段在您向他们发送json时已经打开。 –

+0

我的碎片打开时,我没有发送任何东西,但因为我想发送数据并想要在碎片布局的textview中设置文本,它不工作。 – Saket

变化的片段 -

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

希望这个代码线将工作做好。

+0

它将如何工作?片段已经上了,所以现在你不会得到价值onActivityCreated() –

+0

@jiteshmohite我已经运行的代码,它的工作原理,因为viewPager.setAdapter(适配器)设置参数后调用。所有数据都预先加载。所以当适配器连接这个片段时,它肯定会在ActivityCreated()上彻底完成。 – MMBarno

+0

谢谢,它正在工作:) – Saket

嗨在片段中使用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"); 

使用方法来获得ArrayList的在onCreateView或片段onCreate

+0

您也可以将数据作为json字符串。 –