如何在android的警报对话框中显示json项目列表?

时间:2019-05-22 09:06:05

标签: android json response alertdialog

现在,我有了“警报”对话框的简单代码,并从JSON响应中获取了一个名称列表。我只是想在对话框上设置此响应名称列表。请建议我该怎么做。.

响应为:

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

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

简单警报框代码:

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

1 个答案:

答案 0 :(得分:0)

请尝试使用此代码,可能会对您有所帮助:

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