使用JSON进行动态表布局分页

时间:2017-12-13 08:06:16

标签: android json dynamic pagination android-tablelayout

我正在使用JSON获取和显示数据。使用动态表布局加载数据。我想添加prev / next按钮,所以我在Stack Overflow上找到了一个代码,但没有任何工作。我有大约3000个条目,并希望每页显示100个。错误说:

  

java.lang.IllegalStateException:指定的子级已有父级。您必须首先在孩子的父母上调用removeView()。

我该怎么做?

tv = (TableLayout)findViewById(R.id.table);
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        JSONArray jsonArray = jsonObject.getJSONArray("list");
                        tv.removeAllViewsInLayout();
                        int flag = 1;

                        for (int i =0 ; i <= jsonArray.length();i++) {
                            PaginationLayout paginationLayout = new PaginationLayout(MainActivity.this);
                            paginationLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
                            TableRow tr = new TableRow(MainActivity.this);
                            tr.setLayoutParams(new ViewGroup.LayoutParams(
                                    ViewGroup.LayoutParams.FILL_PARENT,
                                    ViewGroup.LayoutParams.WRAP_CONTENT));

                            if(flag==1) {
                                TextView b6 = new TextView(MainActivity.this);
                                b6.setText("ID");
                                b6.setBackgroundColor(Color.GREEN);
                                b6.setTop(20);
                                b6.setTextColor(Color.BLUE);
                                b6.setTextSize(15);
                                tr.addView(b6);

                                TextView b19 = new TextView(MainActivity.this);
                                b19.setPadding(10, 0, 0, 0);
                                b19.setTextSize(15);
                                b19.setText("name");
                                b19.setTextColor(Color.BLUE);
                                tr.addView(b19);

                                //  jsonObject1.getString("address");
                                TextView b29 = new TextView(MainActivity.this);
                                b29.setPadding(10, 0, 0, 0);
                                b29.setText("address");
                                b29.setTextColor(Color.BLUE);
                                b29.setTextSize(15);
                                tr.addView(b29);

                                tv.addView(tr);
                                final View vline = new View(MainActivity.this);
                                vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2));
                                vline.setBackgroundColor(Color.BLUE);
                                tv.addView(vline);
                                flag = 0;
                            }else {
                                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                                String n = jsonObject1.getString("id");
                                TextView b6 = new TextView(MainActivity.this);
                                b6.setText(n);
                                b6.setTextColor(Color.BLUE);
                                b6.setTextSize(15);
                                tr.addView(b6);

                                TextView b1 = new TextView(MainActivity.this);
                                b1.setPadding(10, 0, 0, 0);
                                b1.setTextSize(15);
                                String stime1 = jsonObject1.getString("name");
                                b1.setText(stime1);
                                b1.setTextColor(Color.WHITE);
                                tr.addView(b1);

                                TextView b2 = new TextView(MainActivity.this);
                                b2.setPadding(10, 0, 0, 0);
                                String stime2 = jsonObject1.getString("address");
                                b2.setText(stime2);
                                b2.setTextColor(Color.RED);
                                b2.setTextSize(15);
                                tr.addView(b2);

                                tv.addView(tr);
                                final View vline1 = new View(MainActivity.this);
                                vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
                                vline1.setBackgroundColor(Color.WHITE);
                                tv.addView(vline1);
                                paginationLayout.addView(tv);
                                // set pagination layout
                                setContentView(paginationLayout);
                            }
                        }

0 个答案:

没有答案