Android TableLayout with pagination: Problem When click the Next/previous TableLayout need to reset

喜夏-厌秋 提交于 2019-12-06 10:27:44

问题


I have 2000 records.I want to show list of products in tablelayout.In the fisrst load need to show 50 records.Rest of the records need to show through pagination.

In onCreate() i have done table Layout: Its Dynamic Table contents

   tl = (TableLayout) findViewById(R.id.tableLayout1);
    if(productList.size() >0){
        if(productList.size() < end){
            end = productList.size();
        }
        Log.i("**onCreate**" , end + "--" + initil);
        stk.setText("Stk " + productList.get(0).getAvailableQuantity());
        for (int i = initil; i <end; i++) {
            Log.i("**i**" ,""+ i);
            TableRow tr = new TableRow(this);
            tr.setTag(i);
            TableLayout.LayoutParams tableRowParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT);
            if(i == initil){
                selectedProductPrice.setText(Double.toString(productList.get(i).getPrice()));
            }
            int leftMargin=10;
            int topMargin=2;
            int rightMargin=10;
            int bottomMargin=2;

            tableRowParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);

            TextView txtCode = new TextView(this);
            txtCode.setTextSize(1, 12);
            createView(tr, txtCode, productList.get(i).getProductCode());

            TextView txtDes = new TextView(this);
            txtDes.setTextSize(1, 12);
            createView(tr, txtDes, productList.get(i).getDescription());

            EditText txtQty = new EditText(this);
            txtQty.setTextSize(2, 12);
            txtQty.setHeight(4);
            txtQty.setWidth(6);
            txtQty.setId(i);
            txtQty.setFocusable(true);
            txtQty.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
            txtQty.setText("0.00");
            tr.addView(txtQty); 

            txtQty.addTextChangedListener(new TextWatcher() {
                public void afterTextChanged(Editable s) { 
                    Log.v("TAG", "afterTextChanged");
                }

                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
                    Log.v("TAG", "beforeTextChanged");
                }

                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    Log.v("TAG", "onTextChanged");
                }
            });

            if(invPriceEdit.equals("") && invPriceEdit.equals("1")){
                EditText txtPrice = new EditText(this);
                txtPrice.setText(Double.toString(productList.get(i).getPrice()));
                txtPrice.setTextSize(1, 12);
                txtPrice.setHeight(3);
                txtPrice.setWidth(7);
                tr.addView(txtPrice); 

            }else{
                TextView txtPrice = new TextView(this);
                txtPrice.setTextSize(1, 12);
                txtPrice.setText(Double.toString(productList.get(i).getPrice()));
            }
            Log.i("----" , Double.toString(productList.get(i).getPrice()));
            TextView txtVal = new TextView(this);
            txtVal.setTextSize(1, 12);
            createView(tr, txtVal,"0.00");

            TextView txtDisVal = new TextView(this);
            txtDisVal.setTextSize(1,12);
            createView(tr, txtDisVal,"0.00");

            TextView txtDisQty = new TextView(this);
            txtDisQty.setTextSize(1, 12);
            createView(tr, txtDisQty,"0");

            tr.setOnClickListener(new View.OnClickListener(){
                 public void onClick(View view){
                   System.out.println("Row Clicked with tag " + view.getTag()); 
                 }
            });
            tl.addView(tr);
        }

And onNextAction() method I did pagination

  public void onNextProductAction(View view) {
    int tmp = productList.size();
    Button next = (Button) findViewById(R.id.next);
    if(end < tmp){
        end = end +10;
        initil = initil +10;
        if(end > tmp ){
            end = productList.size() - initil;
            next.setEnabled(false);
        }
    }else{
        end = productList.size() - initil;
        next.setEnabled(false);
    }
    Log.i("**onNextProductAction**" , "--" + end + "----" + initil);
     }

The problem is When I click the next button TableLaout need to reset the contents.

That means I got list of products using productList = getProducts(altSearch,productCate,productGroup,productType); When I parse the initial value & end value, The it will need to laod particular that records;

When I click next option it attached the records with previous records.

I want to show only next available records only.


回答1:


I think you should try it like this,

productList = all products from db

Show the required products on first page in TableLayout

Now, on Next Button...

Clear TableLayout

int count=table.getChildCount();
     for(int i=0;i<count;i++)
         table.removeView(layout.getChildAt(i));

or

 table.removeAllViews();

show the next products from the productList. (you will be having here initil and end)



来源:https://stackoverflow.com/questions/7315073/android-tablelayout-with-pagination-problem-when-click-the-next-previous-tablel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!