for循环在表中两次显示我的数据

时间:2018-11-29 04:44:44

标签: java android-studio

  for (int j = 0; j < array.length(); j++) {
                            JSONObject c = array.getJSONObject(j);

                            mdate1 = c.getString("date");
                            mshift = c.getString("shift");
                            mqty1 = c.getString("noof_ltrs");
                            mfat1 = c.getString("fat");
                            mrate1 = c.getString("amt_ltrs");
                            mtamt1 = c.getString("total_amount");
                            msts1 = c.getString("Status");



                            TableRow tbrow = new TableRow(getContext());
                            TextView t1v = new TextView(getContext());
                            t1v.setText( mdate1);
                            t1v.setTextColor(Color.WHITE);
                            t1v.setGravity(Gravity.CENTER);
                            tbrow.addView(t1v);
                            TextView t2v = new TextView(getContext());
                            t2v.setText(mshift);
                            t2v.setTextColor(Color.WHITE);
                            t2v.setGravity(Gravity.CENTER);
                            tbrow.addView(t2v);
                            TextView t3v = new TextView(getContext());
                            t3v.setText(mqty1);
                            t3v.setTextColor(Color.WHITE);
                            t3v.setGravity(Gravity.CENTER);
                            tbrow.addView(t3v);
                            TextView t4v = new TextView(getContext());
                            t4v.setText(mfat1);
                            t4v.setTextColor(Color.WHITE);
                            t4v.setGravity(Gravity.CENTER);
                            tbrow.addView(t4v);
                            TextView t5v = new TextView(getContext());
                            t5v.setText(mrate1);
                            t5v.setTextColor(Color.WHITE);
                            t5v.setGravity(Gravity.CENTER);
                            tbrow.addView(t5v);
                            TextView t6v = new TextView(getContext());
                            t6v.setText(mtamt1);
                            t6v.setTextColor(Color.WHITE);
                            t6v.setGravity(Gravity.CENTER);
                            tbrow.addView(t6v);
                            TextView t7v = new TextView(getContext());
                            t7v.setText(msts1);
                            t7v.setTextColor(Color.WHITE);
                            t7v.setGravity(Gravity.CENTER);
                            tbrow.addView(t7v);
                            stk1.addView(tbrow);
}

这是我生成动态表并读取url并将其数据显示到表的地方,这是我的问题,其中for循环使我的表两次显示数据 {{3 }}

2 个答案:

答案 0 :(得分:0)

JSON本身的来源是否具有不同的数据?您应该检查JSON是否存在重复数据。

答案 1 :(得分:0)

我从方法中获得的其他代码就像

    public void  loadProducts(final String idss) {

    String y=url+idss;

    StringRequest stringRequest = new StringRequest(Request.Method.GET, y+"&Month="+m2,
            new Response.Listener<String>() {
                //String shift = "";

                String mdate1 = "0";
                String mqty1 = "0";
                String mfat1 = "0";
                String mrate1 = "0";
                String mtamt1 = "0";
                String msts1 = "0";
                String mshift = "0";



                @Override
                public void onResponse(String response) {


                    try {


                        JSONArray array = new JSONArray(response);



                        for (int j = 0; j < array.length(); j++) {
                            JSONObject c = array.getJSONObject(j);

                            mdate1 = c.getString("date");
                            mshift = c.getString("shift");
                            mqty1 = c.getString("noof_ltrs");
                            mfat1 = c.getString("fat");
                            mrate1 = c.getString("amt_ltrs");
                            mtamt1 = c.getString("total_amount");
                            msts1 = c.getString("Status");



                            TableRow tbrow = new TableRow(getContext());
                            TextView t1v = new TextView(getContext());
                            t1v.setText( mdate1);
                            t1v.setTextColor(Color.WHITE);
                            t1v.setGravity(Gravity.CENTER);
                            tbrow.addView(t1v);
                            TextView t2v = new TextView(getContext());
                            t2v.setText(mshift);
                            t2v.setTextColor(Color.WHITE);
                            t2v.setGravity(Gravity.CENTER);
                            tbrow.addView(t2v);
                            TextView t3v = new TextView(getContext());
                            t3v.setText(mqty1);
                            t3v.setTextColor(Color.WHITE);
                            t3v.setGravity(Gravity.CENTER);
                            tbrow.addView(t3v);
                            TextView t4v = new TextView(getContext());
                            t4v.setText(mfat1);
                            t4v.setTextColor(Color.WHITE);
                            t4v.setGravity(Gravity.CENTER);
                            tbrow.addView(t4v);
                            TextView t5v = new TextView(getContext());
                            t5v.setText(mrate1);
                            t5v.setTextColor(Color.WHITE);
                            t5v.setGravity(Gravity.CENTER);
                            tbrow.addView(t5v);
                            TextView t6v = new TextView(getContext());
                            t6v.setText(mtamt1);
                            t6v.setTextColor(Color.WHITE);
                            t6v.setGravity(Gravity.CENTER);
                            tbrow.addView(t6v);
                            TextView t7v = new TextView(getContext());
                            t7v.setText(msts1);
                            t7v.setTextColor(Color.WHITE);
                            t7v.setGravity(Gravity.CENTER);
                            tbrow.addView(t7v);
                            stk1.addView(tbrow);




                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                        Log.d("error", "onResponse: " + e);
                    }


            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {


                }
            });


    Volley.newRequestQueue(getContext()).add(stringRequest);


}

then this will be my Jason response from URL i don't think the data is repeated too