Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中显示警报对话框上的json项目列表?_Android_Json_Response_Android Alertdialog - Fatal编程技术网

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

如何在android中显示警报对话框上的json项目列表?,android,json,response,android-alertdialog,Android,Json,Response,Android Alertdialog,现在我有了警报对话框的简单代码,并从JSON响应中获得了一个名称列表。我只想在对话框中设置此响应名称列表。请告诉我怎么做 答复是: [ { "id": "1", "Name": "A" }, { "id": "2", "Name": "B" }, { "id": "3", "Name": "C" }, { "id": "4", "Name": "D" } ] 从JSO

现在我有了警报对话框的简单代码,并从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 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);
    }

你有什么错误吗?使用此codecreate对话框,使用listview创建自定义布局,并将json列表数据附加到adapter@NikunjParadva此代码中没有错误。我只想在警报中添加此响应名称列表box@AnasMehar请告诉我这方面的任何代码在这段代码中,你获取静态数据{“Alex”,“Johnny”,“John”,“Ammy”},但我想要列表中的API数据,您知道吗?首先点击api获取所有数据并将相同的数据添加到数组中,然后将该数组传递到函数中,如下面的public void showdialogWithlist(查看视图,ArrayList ArrayList),现在您可以将项添加到该对话框中。
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);
    }