剛剛忍受與我,新的發展,我想加載一個JSON文件在一個Fragment類的XML其視圖分頁器,Tablayout。我的目標是在一個類中加載所有數據,然後在同一個類中通過捆綁包或其他類進行解析後,將這些數據發送到不同的選項卡。我試着用我的方法,如果還有其他方法可以做到這一點,那麼你可以在這裏發佈。深受好評。 你可以看看代碼。加載Json到一個類,並將它們發送到不同的類
nd_home_main_VP.java(負載這裏所有的JSON)
public class nd_home_main_VP extends Fragment {
private TabLayout tabLayout;
private ViewPager viewPager;
private Toolbar toolbar;
private View view=null;
private FrameLayout frameLayout2;
private static final String boom="http://api.androidhive.info/json/movies.json";
private Fragment fragment;
Bundle bundle=new Bundle();
String b;
Bundle bundle2=new Bundle();
String b2;
private FragmentManager fragmentManager=getFragmentManager();
private FragmentTransaction fragmentTransaction;
public nd_home_main_VP() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view= inflater.inflate(R.layout.nd_menu, container, false);
frameLayout2=(FrameLayout)view.findViewById(R.id.fl_id);
viewPager = (ViewPager)view.findViewById(R.id.viewPager_home_id);
setupViewPager(viewPager);
tabLayout = (TabLayout)view.findViewById(R.id.tabLayout_1_home_id);
if (tabLayout != null) {
tabLayout.setupWithViewPager(viewPager);
}
JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(Request.Method.GET, boom, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response_jsonArray) {
Log.d("YES",response_jsonArray.toString());
for(int i=0;i<response_jsonArray.length();i++){
try {
JSONObject jsonObject=response_jsonArray.getJSONObject(i);
b=jsonObject.getString("rating");
bundle.putString("message", b);
Log.d("b",bundle.toString());
b2=jsonObject.getString("releaseYear");
bundle2.putString("message2",b);
Log.d("b2",bundle2.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
});
AppController.getInstance().addToRequestQueue(jsonArrayRequest);
return view;
}
public void doTransaction(){
fragmentTransaction=getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fl_id,fragment);
fragmentTransaction.commit();
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
adapter.addFragment(new OneFragment(), "ONE");
adapter.addFragment(new TwoFragment(), "TWO");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 1:
fragment=new OneFragment();
fragment.setArguments(bundle);
doTransaction();
return fragment;
case 2:
fragment=new TwoFragment();
fragment.setArguments(bundle2);
doTransaction();
return fragment;
default:
}
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
在XML文件中,我得到的FrameLayout,TabLayout和ViewPager
這是這裏
java.lang.IllegalStateException:
無法更改容器ID錯誤的片段 OneFragment{32fbf30a id=0x7f0d007c android:switcher:2131558526:1}: was 2131558524 now 2131558526
我想知道什麼是最好的方法來解決這個問題,現在你可以檢查t他錯了。
如果刪除此項,當您刷新viewpager時,您將如何通過其他選項卡發送數據 – notTdar
創建片段實例時,您必須傳遞數據:請參閱http://www.androiddesignpatterns.com/ 2012/05/using-newinstance-to-instantiate.html – Lino
沒問題,所以當json被加載到一個類時,我應該按照上面的指示將它發送到其他佈局(標籤),然後刷一下viwpager? – notTdar