vendredi 3 janvier 2020

How to add dynamic checkbox in dialog box according to json array length?

I want to add the dynamic checkbox inside dialog box with submit and cancel button and the checkbox should become according to the size of JSON array from API

  private void callDoctorTiming() {
    String request_url = GlobeStrings.BaseUrl + "/doctor-admin/apis/app.php?api=doctorTiming";
    pd = new ProgressDialog(getActivity());
    pd.setMessage(getResources().getString(R.string.Loading));
    pd.show();
    RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
    final String response = null;
    final String finalResponse = response;
    StringRequest postRequest = new StringRequest(com.android.volley.Request.Method.POST,
            request_url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            pd.dismiss();
            Log.e("@@TimeApi", response);
            try {

                JSONObject obj = new JSONObject(response);
                JSONObject objData = obj.getJSONObject("data");
                JSONArray jsonArray = objData.getJSONArray("time");


                if (!obj.getBoolean("error")) {

                    for (int i=0; i<jsonArray.length(); i++){



                        String time = jsonArray.getString(i);
                        Log.e("@@Time", time);



                    }

                  //  profileModel.setAvailability_time(profileModel.getAvailability_time() + "");
                   /* Toast.makeText(getActivity(), obj.getString("message"), Toast.LENGTH_SHORT).show();
                    onDateTime = obj.getString("data");
                    Log.e("@@ondateTime", onDateTime);*/
                } else {
                    Toast.makeText(getActivity(), obj.getString("message"), Toast.LENGTH_SHORT).show();
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    pd.dismiss();
                    Log.e(TAG, "error in api " + error.getMessage());
                }
            }
    ) {
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();
            params.put("doctor_id", "" + doctorID);
            params.put("available_date", "01-01-2020");
           // params.put("available_date", "" + date);
            return params;
        }
    };
    postRequest.setRetryPolicy(new DefaultRetryPolicy(0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    requestQueue.add(postRequest);
}

here in response, I am getting time inside response I want to add dynamic checkbox with time as I am getting from the response

here is my response from API

{
"error": false,
"data": {
    "time": [
        "10:00 am-04:00 pm"
    ]
},
"message": "success"
 }

here is my dialog layout :

  void showTimeDialog() {
    final Dialog dialog = new Dialog(getContext());
    dialog.setContentView(R.layout.time_layout);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    TextView tv_pcolse = (TextView) dialog.findViewById(R.id.tv_pcolse);
    TextView tv_submit = (TextView) dialog.findViewById(R.id.tv_submit);
    final CheckBox checkbox_time1 = (CheckBox) dialog.findViewById(R.id.checkbox_time1);
    checkbox_time1.setText("");

    tv_pcolse.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
    tv_submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (checkbox_time1.isChecked())
                profileModel.setAvailability_time(profileModel.getAvailability_time() + "" + 
         checkbox_time1.getText());
            else
                profileModel.setAvailability_time(profileModel.getAvailability_time() + "");

            dialog.dismiss();
        }
    });
    dialog.show();
}

here is my xml of dialog :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="340dp"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="@drawable/dialog_design">

<TextView
    android:layout_width="230dp"
    android:layout_height="wrap_content"
    android:text="@string/Whattimedoyouacceptonlinevisit"
    android:layout_gravity="center"
    android:gravity="center"
    android:textSize="20dp"
    android:textColor="#444141"
    android:layout_marginTop="20dp"/>
<CheckBox
    app:buttonTint="@color/dark_color"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="15dp"
    android:textSize="18sp"
    android:clickable="true"
    android:layoutDirection="rtl"
    android:textColor="#888282"
    android:textStyle="bold"
    android:id="@+id/checkbox_time1"
    android:layout_marginRight="30dp"/>

<LinearLayout
    android:layout_margin="15dp"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:orientation="horizontal"
    android:background="#FF68B6DA"
    android:weightSum="2"
    android:gravity="center"
    android:layout_marginTop="10dp">

    <TextView
        android:id="@+id/tv_submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/Submit"
        android:textColor="#fff"
        android:textSize="18dp"
        android:gravity="center"/>

    <View
        android:layout_width="1dp"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:layout_marginTop="12dp"
        android:layout_marginBottom="12dp"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/Close"
        android:textColor="#fff"
        android:textSize="18dp"
        android:gravity="center"
        android:id="@+id/tv_pcolse"/>

</LinearLayout>



Aucun commentaire:

Enregistrer un commentaire