如何在android警告对话框中显示json解析数据?

时间:2017-08-02 07:42:42

标签: android json android-alertdialog

我必须将java脚本对象表示法解析数据显示到android的警告对话框中。可能吗?如果我们看到Play商店应用程序,如果我们想购买或订阅一些电影,那么它会显示警告对话框,以及电影横幅缩略图,电影名称,价格和继续按钮。此外,如果单击“继续”按钮,则会打开另一个对话框。所以任何人都可以帮助我,如何创建它?这是游戏商店的示例图像。我想在我的应用程序中创建这种类型的对话框。请建议我天气可能与否。 enter image description here

这是代码

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.cartBookDescription);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          inputDialog()
        }
    });

 void inputDialog() {


    try {


       textView.setText("my order");
       dbhelper.openDataBase();
    } catch (SQLException sqle) {
        throw sqle;
    }

    LayoutInflater layoutInflater = LayoutInflater.from(this);
    View alertDialog = layoutInflater.inflate(R.layout.alert_dialog, null);

    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setView(alertDialog);



    alert.setTitle("Order Now");

    alert.setCancelable(false);
  alert.setView(edtQuantity);

    alert.setPositiveButton("Subscribe Now", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

            int quantity = 0;


         } else {
                dialog.cancel();
          }
        }
    });

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {


            dialog.cancel();
        }
    });

    alert.show();
}
public void parseJSONData() {

    try {
        // request data from menu detail API
        HttpClient client = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout((HttpParams) client.getParams(), 15000);
        HttpConnectionParams.setSoTimeout((HttpParams) client.getParams(), 15000);
        HttpUriRequest request = new HttpGet(MenuDetailAPI);
        HttpResponse response = client.execute(request);
        InputStream atomInputStream = response.getEntity().getContent();


        BufferedReader in = new BufferedReader(new InputStreamReader(atomInputStream));

        String line;
        String str = "";
        while ((line = in.readLine()) != null) {
            str += line;
        }


        JSONObject json = new JSONObject(str);
        JSONArray data = json.getJSONArray("Book"); // this is the "items: [ ] part

        for (int i = 0; i < data.length(); i++) {
            JSONObject object = data.getJSONObject(i);


            Detail_id = object.getString("id");
            Detail_audio = object.getString("audiofile");
            Detail_bookauthor = object.getString("bookauthor");
            Detail_image = object.getString("photo");
            Detail_name = object.getString("bookname");
            Detail_category = object.getString("bookcategory");
            Detail_premium = object.getString("premiumtype");
            Detail_price = Double.valueOf(formatData.format(object.getDouble("price")));
            Detail_validity = Integer.valueOf((object.getInt("validity")));             
            Detail_description = object.getString("description");

        }


    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (JSONException | IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

parseJson方法是在asyncTask下编写的。 Json数据正在正确解析,但当我试图在警告对话框中显示该数据时,警报对话框将崩溃。

2 个答案:

答案 0 :(得分:0)

是的,你可以。您只需要通过创建活动和布局类来解析数据并创建自定义对话框。可以看到示例here

答案 1 :(得分:0)

您必须为自定义视图创建xml文件,并根据您的要求添加要在警告对话框中显示的内容,请参阅此链接How to create a Custom Dialog box in android? 要么 https://bhavyanshu.me/tutorials/create-custom-alert-dialog-in-android/08/20/2015