gpt4 book ai didi

android - 使用 android volley 将 json 数组作为包传递给 fragment

转载 作者:行者123 更新时间:2023-11-29 15:44:04 24 4
gpt4 key购买 nike

我想制作一个从 android volley 获取数据的 viewpager。我已经解析了 json,如下所示。

    HashMap<String, String> event = new HashMap<String, String>();

JSONArray timings = object1.getJSONArray("periods");


for (int t=0; t<timings.length(); t++){
JSONObject timeObject = timings.getJSONObject(t);

String period = timeObject.getString("periodName");
periodList.put("",period);

System.out.println(period);

event.put(TAG_PERIOD,period);

}

现在我想发送特定日期(周一、周二等)的类时间,如下所示:

resulting image

我想将解析后的数组发送到 fragment 。以下是 pageradapter 和 fragment 类的代码。

页面适配器:

    public class PagerAdapter extends FragmentPagerAdapter {
int mNumOfTabs;
TabFragment1 fragA, fragB,fragC,fragD,fragE,fragF;


public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}

public void setFragments(Context c){

// Set up the simple base fragments
fragA = new TabFragment1();
fragB = new TabFragment1();
fragC = new TabFragment1();
fragD = new TabFragment1();
fragE = new TabFragment1();
fragF = new TabFragment1();

}

@Override
public Fragment getItem(int position) {

Fragment fragment;

switch (position) {
case 0:
fragment = fragA;
return fragment;

case 1:
fragment = fragB;
return fragment;

case 2:
fragment = fragC;
return fragment;

case 3:
fragment = fragD;
return fragment;

case 4:
fragment = fragE;
return fragment;

case 5:
fragment = fragF;
return fragment;

default:
return null;
}


}

@Override
public int getCount() {
return mNumOfTabs;
}

fragment :

    public class TabFragment1 extends android.support.v4.app.Fragment {


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

return inflater.inflate(R.layout.tab_fragment_1, container, false);


}



}

我是 android 的新手,想知道如何将解析后的数据传递给 fragment 。

最佳答案

Bundle args = new Bundle();
args.putString("response",jsonarray.toString());
ListFrag newFragment = new ListFrag ();
newFragment.setArguments(args);

并在 Fragment 中获取 bundle 的值,

Bundle b = getArguments();
String s = b.getString("response");
JSONArray array=new JSONArray(s);

关于android - 使用 android volley 将 json 数组作为包传递给 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35570920/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com