【问题标题】:How to send JSON data from Activity to Fragment?如何将 JSON 数据从 Activity 发送到 Fragment?
【发布时间】:2017-04-22 10:52:44
【问题描述】:

我正在尝试从 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;
    }
}

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

【问题讨论】:

  • 当您向他们发送 json 时,您的片段是否已经打开。
  • 我的片段在我没有发送任何内容时打开,但是当我尝试发送数据并想在片段布局的 textview 中设置文本时它不起作用。

标签: android json android-fragments android-activity


【解决方案1】:

在你的片段中更改这行代码-

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

希望一切顺利。

【讨论】:

  • 它将如何工作?片段已经在顶部,所以现在你不会在 onActivityCreated() 中获得价值
  • @jiteshmohite 我已经运行了代码并且它可以工作,因为 viewPager.setAdapter(adapter) 在设置参数后被调用。并且所有数据都是预加载的。因此,当适配器附加片段时,它肯定会彻底 onActivityCreated()。
  • @Saket 快乐是我的。 :) 如果对您有帮助,请尝试接受答案。
  • 我是新来的,并试图接受,但它要求 15 + 声望。很抱歉,我什至不太了解它所询问的声誉。
  • 没关系。 :D 我只是在开玩笑。我很高兴它对你有帮助。谢谢。 :)
【解决方案2】:

Hi 在片段中使用 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()方法获取Fragment中onCreateViewonCreate中的arrayList。

【讨论】:

  • 你也可以将数据作为json字符串。
猜你喜欢
  • 2013-09-27
  • 1970-01-01
  • 1970-01-01
  • 2021-02-07
  • 1970-01-01
  • 2018-04-14
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多