子片段内的父片段arrayList

时间:2015-11-25 10:37:11

标签: android

我有一个RideHistoryFragment,其ArrayList也有三个子列表。

我的父片段代码如下:

   public class RideHistoryFragment extends Fragment implements
        ApplicationConstants {

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    SharedPreferences myPrefs;
    public ArrayList<RideHistoryResposeBeans> arrayList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_ride_history,
                container, false);

        viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);

        tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);


        arrayList = new ArrayList<RideHistoryResposeBeans>();
          myPrefs = getActivity().getSharedPreferences("login",
                    getActivity().MODE_PRIVATE);
            RideHistoryRequest params = new RideHistoryRequest();
            params.setUserId(myPrefs.getString(REG_ID, ""));
            RideHistoryAsyn asyn = new RideHistoryAsyn();
            asyn.execute(params);

        // Inflate the layout for this fragment
        return rootView;
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);


    }
    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
        adapter.addFragment(new AllRides(arrayList), "ALL RIDES");
        adapter.addFragment(new UpComing(arrayList), "UPCOMING");
        adapter.addFragment(new Completed(arrayList), "COMPLETED");
        viewPager.setAdapter(adapter);

    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<Fragment>();
        private final List<String> mFragmentTitleList = new ArrayList<String>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            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);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }



    public class RideHistoryAsyn extends AsyncTask<Object, Void, Object>
                implements OnCancelListener {
            // private static final Logger LOGGER =
            // Logger.getLogger(HTTPAsyncServiceTask.class);

            private String progressMsg = "Please wait..";
            private ProgressHUD mProgressHUD;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                mProgressHUD = ProgressHUD.show(getActivity(), progressMsg, true,
                        false, this);
                // activity.displayManager.showProgressBar(activity, progressMsg);
            }

            @Override
            protected Object doInBackground(Object... params) {
                Object responseObject = null;
                try {
                    RideHistoryRequest object = (RideHistoryRequest) params[0];

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

                    JSONObject Detail = new JSONObject();
                    try {
                        Detail.put(USERID, object.getUserId());

                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    String url=BASE_URL + RIDE_HISTORY+USERID+"="+myPrefs.getString(REG_ID, "");

                    HttpConnectionManager httpConnManager = new HttpConnectionManager();
                    responseObject = httpConnManager.performGETCall(
                            url,
                            postDataParams);
                    Log.d("url", url);
                } catch (Exception e) {
                    // LOGGER.error("Error occured inside doInBackground ", e);
                }

                return responseObject;
            }

            @Override
            protected void onPostExecute(Object result) {
                super.onPostExecute(result);
                mProgressHUD.dismiss();
                String string=(String) result;

                try {
                    JSONObject jsonObject = new JSONObject(string);
                    JSONArray responseArray = jsonObject.getJSONArray("response");
                    JSONArray historyeArray = jsonObject.getJSONArray("history");
                    JSONObject object = responseArray.getJSONObject(0);
                    Geocoder geocoder = new Geocoder(getContext());
                    String message = object.optString("Message");
                    if (message.equals("Record found")) {
                        arrayList.clear();
                        for (int i = 0; i < historyeArray.length(); i++) {
                            JSONObject jsonObject2 = historyeArray.getJSONObject(i);

                            RideHistoryResposeBeans beans = new RideHistoryResposeBeans();
                            beans.setBookingStatus(jsonObject2
                                    .optString("BookingStatus"));
                            beans.setCabName(jsonObject2.optString("CabName"));
                            beans.setCabType(jsonObject2.optString("CabType"));
                            beans.setUserId(jsonObject2.optString("UserId"));
                            beans.setUserLat(jsonObject2.optString("UserLat"));
                            beans.setUserLong(jsonObject2.optString("UserLong"));

                            List<Address> addresses = null;
                            String addressText = "";

                            try {
                                addresses = geocoder.getFromLocation(Double
                                        .parseDouble(jsonObject2
                                                .optString("UserLat")), Double
                                        .parseDouble(jsonObject2
                                                .optString("UserLong")), 1);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }

                            if (addresses != null && addresses.size() > 0) {
                                Address address = addresses.get(0);

                                addressText = String
                                        .format("%s, %s, %s",
                                                address.getMaxAddressLineIndex() > 0 ? address
                                                        .getAddressLine(0) : "",
                                                address.getLocality(), address
                                                        .getCountryName());
                            }

                            beans.setFromAddress(addressText);
                            arrayList.add(beans);
                        }
                    }

                } catch (Exception e) {

                }
                setupViewPager(viewPager);
                tabLayout.setupWithViewPager(viewPager);
            }

            @Override
            public void onCancel(DialogInterface dialog) {
                // TODO Auto-generated method stub

            }
        }

    }

我有三个孩子,现在我想在其子片段中使用ArrayList。 我在子片段中使用下面的代码,但AllRides(子片段)中的ArrayList给出了空点异常。

 public class AllRides extends Fragment{
    ArrayList< RideHistoryResposeBeans> arrayList;
    RideRecyclerAdapter adapter;
    private RecyclerView mRecyclerView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.all_ride,
                container, false);

         mRecyclerView = (RecyclerView) rootView.findViewById(R.id.rv);
         arrayList=new ArrayList<RideHistoryResposeBeans>();
        // arrayList=((RideHistoryFragment)getParentFragment()).arrayList;
         adapter=new RideRecyclerAdapter(getContext(),arrayList );
        // Inflate the layout for this fragment
         Log.d("size", arrayList.size()+"");
        mRecyclerView.setAdapter(adapter);
        return rootView;
    }
public AllRides(ArrayList< RideHistoryResposeBeans> arrayList) {
    // TODO Auto-generated constructor stub
    this.arrayList=arrayList;
}

...谢谢

0 个答案:

没有答案