2017-12-12 16 views
0

私は動的にボタンを読み込んでいます。現在私は2つのボタンがあります。最初のボタンをクリックすると、新しいアクティビティが読み込まれます。同じアクティビティが、動的にロードされるすべてのボタンのonclick()のためにロードされ、そのアクティビティデータに関連するbuttonが表示されます。ボタンのOnclick()は、dbからの特定の値のみを取得して表示する必要があります。Android volley json parsing

しかしlogcatショー"NO VALUE FOR LIST".

StringRequest stringRequest = new StringRequest(Request.Method.GET, url, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        progressDialog.dismiss(); 
        try { 
         JSONObject jsonObject = new JSONObject(response); 
         JSONArray jsonArray = jsonObject.getJSONArray("list"); 
         tv.removeAllViewsInLayout(); 
         int flag = 1; 
         for (int i =-1 ; i < jsonArray.length();i++) { 
          TableRow tr = new TableRow(TestDetails.this); 
          tr.setLayoutParams(new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT, 
            ViewGroup.LayoutParams.WRAP_CONTENT)); 
          if (flag == 1) { 
           TextView b10 = new TextView(TestDetails.this); 
           b10.setPadding(50, 30, 50, 30); 
           b10.setText("ID"); 
           b10.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START); 
           b10.setTop(20); 
           b10.setTextColor(Color.BLACK); 
           b10.setBackgroundColor(Color.rgb(206, 147, 216)); 
           b10.setTextSize(15); 
           tr.addView(b10); 

           TextView b6 = new TextView(TestDetails.this); 
           b6.setPadding(50, 30, 50, 30); 
           b6.setText("TEST CODE"); 
           b6.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START); 
           b6.setTop(20); 
           b6.setTextColor(Color.BLACK); 
           b6.setBackgroundColor(Color.rgb(206, 147, 216)); 
           b6.setTextSize(15); 
           tr.addView(b6); 

           TextView b19 = new TextView(TestDetails.this); 
           b19.setPadding(50, 30, 50, 30); 
           b19.setTextSize(15); 
           b19.setText("TEST DESCRIPTION"); 
           b19.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START); 
           b19.setTextColor(Color.BLACK); 
           b19.setBackgroundColor(Color.rgb(206, 147, 216)); 
           tr.addView(b19); 

           TextView b29 = new TextView(TestDetails.this); 
           b29.setPadding(10, 0, 0, 0); 
           b29.setText("TEST PRICE"); 
           b29.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 
           b29.setTextColor(Color.rgb(104, 143, 202)); 
           b29.setTextSize(15); 
           tr.addView(b29); 

           tv.addView(tr); 
           final View vline = new View(TestDetails.this); 
           vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 10)); 
           vline.setBackgroundColor(Color.BLUE); 
           tv.addView(vline); 
           flag = 0; 
          } else { 
           JSONObject jsonObject1 = jsonArray.getJSONObject(i); 

           String n = jsonObject1.getString("test_code"); 
           TextView b6 = new TextView(TestDetails.this); 
           b6.setText(n); 
           b6.setPadding(50, 30, 50, 30); 
           b6.setTextColor(Color.BLACK); 
           b6.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START); 
           b6.setTextSize(15); 
           tr.addView(b6); 

           TextView b1 = new TextView(TestDetails.this); 
           b1.setPadding(50, 30, 50, 30); 
           b1.setTextSize(15); 
           String stime1 = jsonObject1.getString("test_description"); 
           b1.setText(stime1); 
           b1.setTextColor(Color.BLACK); 
           // b1.setBackgroundColor(Color.rgb(186, 104, 200)); 
           b1.setTextAlignment(View.TEXT_ALIGNMENT_VIEW_START); 
           tr.addView(b1); 

           TextView b2 = new TextView(TestDetails.this); 
           b2.setPadding(10, 30, 110, 30); 
           String stime2 = jsonObject1.getString("test_price"); 
           b2.setText(stime2); 
           b2.setTextColor(Color.BLACK); 
           b2.setBackgroundColor(Color.rgb(144, 202, 249)); 
           b2.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 
           b2.setTextSize(15); 
           tr.addView(b2); 

           tv.addView(tr); 
           final View vline1 = new View(TestDetails.this); 
           vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1)); 
           vline1.setBackgroundColor(Color.rgb(1, 132, 143)); 
           tv.addView(vline1); 
          } 
         } 


        } catch (JSONException e) { 
         progressDialog.dismiss(); 
         Toast.makeText(getApplicationContext(),"Unable to display data. Check internet connection",Toast.LENGTH_LONG); 
         Log.e("Error", "Failed" +e.toString()); 
         e.printStackTrace(); 
        } 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        progressDialog.dismiss(); 
        Toast.makeText(getApplicationContext(),"Unable to display data. Check internet connection", Toast.LENGTH_LONG); 
        Log.e("Error", "Try Later" +error.toString()); 

       } 
      }); 
    RequestQueue requestQueue = Volley.newRequestQueue(TestDetails.this); 
    requestQueue.add(stringRequest); 

答えて

0

"LIST FOR NO VALUE"。最初の

  • デバッグ。チェックJSON RESPONSE
  • セクションtry {最初BREAK-POINTを追加します。
  • スタートi0

FYI

for (init; condition; increment) { 

} 

initステップが最初に実行し、一度だけれます。このステップでは、 にループ制御変数を宣言して初期化することができます。

for (int i =0 ; i < jsonArray.length();i++) 
    { 
    .............. 
+0

あなたは詳しく説明できますか?私は恐れます。 –

+0

@AparnaMutnalkarまず、JSONレスポンスをチェックしてください。 –

+1

いいえ、私は0からループを開始することを意味します。私は試してみます –