如何将数组列表数据从一个对话框片段传递到另一个对话框片段?

时间:2017-12-14 11:49:23

标签: android android-dialogfragment

经过长时间的互联网搜索,我找不到任何解决方案来满足我的要求。 我有一个数组列表,它被传递到对话框片段,但我的要求是: 当用户选择日期时,它再次调用Web服务并根据数据获取另一个数据并传递相同的对话框片段。我怎样才能做到这一点?我试过静态值,但它没有清除值。我的代码是:

public static class DatePickerDialogFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener
{
    String userName;


    public interface UserUserIdListener {
        void updateIdUserName(String userName);


    }
    public static final int FLAG_START_DATE = 0;
    public static final int FLAG_END_DATE = 1;
   public  static  List<Orederlist>reportorderIdList=new ArrayList<>();
   // private   List<Userlist> reportUserList = new ArrayList<>();


    private int flag = 0;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Calendar calendar = Calendar.getInstance();

        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void setFlag(int i) {
        flag = i;
    }


    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        Calendar calendar = Calendar.getInstance();
        Calendar calendar1=Calendar.getInstance();
        calendar1.set(year, monthOfYear, dayOfMonth);
        calendar.set(year, monthOfYear, dayOfMonth);
        String  reportOrderIds="";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        if (flag == FLAG_START_DATE) {
            textviewFromDate.setText(format.format(calendar.getTime()));
            fromDateReport=textviewFromDate.getText().toString();

            JSONObject pieObject = new JSONObject();
            try
            {
                pieObject.put("fromDate",""+fromDateReport);
                pieObject.put("orderId",""+reportOrderIds);
                pieObject.put("password",""+sPrefs.getString(Constants.PASSWORD,""));
                pieObject.put("toDate",""+toDateReport);
                pieObject.put("user",""+sPrefs.getString(Constants.USER_NAME,""));
                pieObject.put("subUser",""+reportSubUsers);
                progressDialog = new ProgressDialog(getContext());
                progressDialog.setMessage("Loading....");
                progressDialog.setCancelable(false);
                progressDialog.show();



            } catch (Exception e)
            {
                e.printStackTrace();
            }
            // String TAG = "delivered";
            RequestQueue requestQueue = Volley.newRequestQueue(getContext());
            JsonObjectRequest jsOnbjRequest = new
                    JsonObjectRequest(Request.Method.POST,
                            Constants.GetSummarizedReports, pieObject,
                            new Response.Listener<JSONObject>() {
                                @Override
                                public void onResponse(JSONObject response)
                                {
                                    progressDialog.dismiss();
                                    try {
                                        JSONArray jsonArray = (JSONArray) response.get("orderlist");
                                        {
                                            if (jsonArray.length()>0)
                                            {
                                                for (int i = 0; i < jsonArray.length(); i++) {
                                                    JSONObject json = jsonArray.getJSONObject(i);
                                                    // Integer name = json.optInt();
                                                    String name = json.optString("orderId").toString();
                                                    Orederlist user = new Orederlist(name);
                                                    user.setOrderId(name);
                                                    reportorderIdList.add(user);

                                                }}
                                        }
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }

                                }
                            },

                            new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError error) {
                                    progressDialog.dismiss();

                                }
                            }) {
                        @Override
                        public String getBodyContentType() {
                            return "application/json";
                        }

                    };
            jsOnbjRequest.setRetryPolicy(new DefaultRetryPolicy(500000,
                    DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

            requestQueue.add(jsOnbjRequest);

        } else if (flag == FLAG_END_DATE) {
            textviewToDate.setText(format.format( calendar1.getTime()));
            toDateReport= textviewToDate.getText().toString();
            JSONObject pieObject = new JSONObject();

            try
            {
                pieObject.put("fromDate",""+fromDateReport);
                pieObject.put("orderId",""+reportOrderIds);
                pieObject.put("password",""+sPrefs.getString(Constants.PASSWORD,""));
                pieObject.put("toDate",""+toDateReport);
                pieObject.put("user",""+sPrefs.getString(Constants.USER_NAME,""));
                pieObject.put("subUser",""+reportSubUsers);

                progressDialog = new ProgressDialog(getContext());
                progressDialog.setMessage("Loading....");
                progressDialog.setCancelable(false);
                progressDialog.show();



            } catch (Exception e)
            {
                e.printStackTrace();
            }
            // String TAG = "delivered";
            RequestQueue requestQueue = Volley.newRequestQueue(getContext());
            JsonObjectRequest jsOnbjRequest = new
                    JsonObjectRequest(Request.Method.POST,
                            Constants.GetSummarizedReports, pieObject,
                            new Response.Listener<JSONObject>() {
                                @Override
                                public void onResponse(JSONObject response)
                                {
                                    progressDialog.dismiss();

                                    try {
                                        JSONArray jsonArray = (JSONArray) response.get("orderlist");
                                        {
                                            if (jsonArray.length()>0)
                                            {
                                                for (int i = 0; i < jsonArray.length(); i++) {
                                                    JSONObject json = jsonArray.getJSONObject(i);
                                                    // Integer name = json.optInt();
                                                    String name = json.optString("orderId").toString();
                                                    Orederlist user = new Orederlist(name);
                                                    user.setOrderId(name);
                                                    reportorderIdList.add(user);

                                                }


                                            }
                                        }
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            },

                            new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError error) {
                                    progressDialog.dismiss();

                                }
                            }) {
                        @Override
                        public String getBodyContentType() {
                            return "application/json";
                        }

                    };
            jsOnbjRequest.setRetryPolicy(new DefaultRetryPolicy(500000,
                    DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

            requestQueue.add(jsOnbjRequest);

        }
    }


}

和对话框片段传递数据是:

FragmentManager managerOrderIds = getSupportFragmentManager();
            UserJobIds dialogOrderIds = new UserJobIds();
            dialogOrderIds.setdata(orderIdList,reportorderIdList);//with static array list which is not claer the value
            dialogOrderIds.show(managerOrderIds, "Dialog");

1 个答案:

答案 0 :(得分:0)

你可以做的只是创建一个newInstance of Dialog Fragment为其分配数据,然后调用show()。下面是一个例子。

public class D2Fragment extends DialogFragment {
private ArrayList<String> list;
public static D2Fragment getInstanceFor(ArrayList<String> list){
    D2Fragment d2Fragment=new D2Fragment();
    d2Fragment.list=list;
    return d2Fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.item_dialog, container,
            false);
    getDialog().setTitle("DialogFragment Sample");
    Log.e("size",list.size()+"");// Here is the list 
    // Do something else
    return rootView;
}

}

显示对话框时。

 ArrayList<String> list=new ArrayList<>();
    list.add("Val1");
    list.add("Val2");
    D2Fragment d2Fragment=D2Fragment.getInstanceFor(list);
    d2Fragment.show(getSupportFragmentManager(),"dialog2");

只需为您的数据创建一个setter。

public class D2Fragment extends DialogFragment {
private ArrayList<String> list;
public void setData(ArrayList<String> list){
    this.list=list;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.item_dialog, container,
            false);
    getDialog().setTitle("DialogFragment Sample");
    Log.e("size",list.size()+"");// Here is the list
    // Do something else
    return rootView;
}

}

并显示Dialog。

 ArrayList<String> list = new ArrayList<>();
    list.add("Val1");
    list.add("Val2");
    D2Fragment d2Fragment = new D2Fragment();
    d2Fragment.setData(list);
    d2Fragment.show(getSupportFragmentManager(), "dialog2");

在活动中,您始终可以找到您在交易时分配的特定DialgFragment标签。

 Fragment fragment=getSupportFragmentManager().findFragmentByTag("dialog2");
            if(fragment!=null && fragment instanceof D2Fragment){
                ((D2Fragment)fragment).call();// You can call any public method of this dialog fragment here
            }