2017-08-02 120 views
0

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

这里是

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数据解析正常,但当我试图在警报对话框中显示数据时,警报对话框会崩溃。

+0

您是否尝试过的东西?任何想法? – xAqweRx

+0

是的,我已经创建了一个XML布局文件,并在警报对话框中充气。但是我想在其中显示json数据。该布局包含一个imageview和两个textview –

+0

@xAqweRx任何帮助? –

回答

0

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

+0

它只显示自定义对话框。请建议我在其中显示json数据。 –

+0

你成功解析了数据吗?如果是,那么你可以通过捆绑传递它。以下是http://android-coding.blogspot.co.id/2012/05/custom-dialog-with-data-passed-in.html示例。 – Kharda