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();
}