簡體   English   中英

如何在android警報對話框中顯示json解析數據?

[英]how can i display json parse data in android alert dialog box?

我必須將Java腳本對象符號顯示為將數據解析到android的警報對話框中。 可能嗎? 如果我們看到Play商店應用程序,並且想購買或訂購一些電影,則它會顯示警報對話框,以及電影橫幅縮略圖,電影名稱,價格和繼續按鈕。 此外,如果單擊繼續按鈕,則會打開另一個對話框。 所以任何人都可以幫助我,如何創建它? 這是Play商店的示例圖片。 我想在我的應用程序中創建這種類型的對話框。 請暗示我是否有可能。 在此處輸入圖片說明

這是代碼

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數據正在正確解析,但是當我嘗試在警報對話框中顯示該數據時,警報對話框崩潰了。

是的你可以。 您只需要解析數據並通過創建活動和布局類來創建自定義對話框。 例子可以在這里看到

您必須為自定義視圖創建xml文件,並根據需要添加要在警報對話框中顯示的內容,請參考此鏈接。 如何在android中創建自定義對話框? https://bhavyanshu.me/tutorials/create-custom-alert-dialog-in-android/08/20/2015

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM