简体   繁体   中英

Android volley json parsing

I am loading buttons dynamically. At present I have 2 buttons. so when I click on 1st button it loads a new activity. Same activity is loaded for onclick() of all the buttons that are dynamically loaded and in that activity data related to that button should be displayed. Onclick() of the button only particular values from db should be fetched and displayed.

But the logcat shows "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);

"NO VALUE FOR LIST".

  • Debug at first. Add BREAK-POINT on first try { section
  • Check JSON RESPONSE .
  • You should START i value 0 .

FYI

for ( init; condition; increment ) {

}

The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM