【问题标题】:Change JSON object index between Fragments更改片段之间的 JSON 对象索引
【发布时间】:2017-09-18 10:39:19
【问题描述】:

我希望在我的片段之间的 QueryUtils.java 文件中更改 JSON 对象索引。

这条线在这里更准确:

JSONObject currentDay = dayArray.getJSONObject(0);

我有 7 个片段,我想将每个片段的索引从 0 更改为 1、2 等等,在搜索了相当多的白色之后,我实际上找不到这样做的方法。

【问题讨论】:

  • 你能发布你的代码吗?
  • @akhilesh0707 好吧,没什么可展示的,但尝试失败了。我基本上创建了一个片段适配器来覆盖片段 getItem 方法并为每个片段设置一个编号位置,最初认为我可以在我的查询工具中使用它来显示活动片段并更改索引。

标签: android json android-fragments


【解决方案1】:

也许这对你有帮助

public class MyFragment extends Fragment {

  private static final String KEY = "pos_id";

  public static MyFragment newInstance(int pos) {

      MyFragment myFragment = new MyFragment();

      Bundle args = new Bundle();
      args.putInt(KEY, pos);
      myFragment.setArguments(args);

      return myFragment;
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    int pos = getArguments().getInt(KEY)
    //TODO ur Query
  }

}

【讨论】:

  • 我同意这个解决方案。在创建时将索引传递给片段并让片段查询它自己的数据,或者如果您已经拥有它,则将数据传递给片段 (public static MyFragment newInstance(JSONObject currentDay){...})。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-18
  • 2015-09-28
  • 2015-12-09
  • 2019-10-18
相关资源
最近更新 更多