-2

Now I have simple code for Alert Dialog Box and get a name list from JSON response. Simply I want to set this responsive name list on Dialog Box. please suggest me how to do this..

Response is:

[
    {
    "id": "1",
    "Name": "A"
    },
    {
    "id": "2",
    "Name": "B"
    },
    {
    "id": "3",
    "Name": "C"
    },
    {
    "id": "4",
    "Name": "D"
    }
]

Fetch data from JSON:

 private void loadReasonData() {
    ArrayList<String>ReasonName = new ArrayList<>();
    RequestQueue requestQueue=Volley.newRequestQueue(getContext());
    StringRequest stringRequest=new StringRequest(Request.Method.POST, url_name, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try{
                JSONArray jsonArray = new JSONArray(response);
                for(int i=0; i<jsonArray.length();i++){
                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                    String orderReason = jsonObject1.getString("Name");
                    ReasonName.add(orderReason);
                }
            }catch (JSONException e){e.printStackTrace();}
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });
    requestQueue.add(stringRequest);
}

Simple Alert box code:

 public void alertListView() {
    final CharSequence[] items;
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Select Reason:");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) {
            Toast.makeText(getActivity(),"Name: " + items[item] , Toast.LENGTH_SHORT).show();
            dialog.dismiss();
        }
    }).show();
}
Parveen Haade
  • 76
  • 1
  • 13

1 Answers1

0

Please try this code may be this will be helpful for you :

public void showdialogWithlist(View view) {

        final String[] items = {"Alex", "Johnny", "John", "Ammy"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("List of Items")

                .setItems(items, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(), items[which] + " is clicked", Toast.LENGTH_SHORT).show();
                    }
                });

        builder.setPositiveButton("OK", null);
        builder.setNegativeButton("CANCEL", null);
        builder.setNeutralButton("NEUTRAL", null);
        builder.setPositiveButtonIcon(getResources().getDrawable(android.R.drawable.ic_menu_call, getTheme()));
        builder.setIcon(getResources().getDrawable(R.drawable.jd, getTheme()));

        AlertDialog alertDialog = builder.create();

        alertDialog.show();

        Button button = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
        button.setBackgroundColor(Color.BLACK);
        button.setPadding(0, 0, 20, 0);
        button.setTextColor(Color.WHITE);
    }
  • In this code You take static data {"Alex", "Johnny", "John", "Ammy"}, but i want to API data in the list, Do you know about it – Parveen Haade May 22 '19 at 09:29
  • So first hit the api and get all the data and add the same data into the array than pass that array into the function like this public void showdialogWithlist(View view,ArrayList arrayList) and further now you can add item to the dialog. – Rajat Kumar Tyagi May 22 '19 at 10:36