视图未从 TableLayout 中删除

Views are not Removed from TableLayout

我有线性布局,我正在以编程方式向其中添加表格布局和行。

当我第一次调用方法时,它工作正常并且显示给定行数的数据。

下次调用时,之前的行仍然可见,并且新数据添加到之前数据的下方。我尝试使用所有删除视图的方法,但仍然无法摆脱旧行。请帮助。

我的Table代码:

 private void maketripsumtable(String pname) {


        if (((LinearLayout) sumtablelay).getChildCount() > 0) {
            ((LinearLayout) sumtablelay).removeAllViews();
            Log.i(TAG, "Views " + sumtablelay.getChildCount() + tableLayout1.getChildCount());
            tableLayout1.removeAllViewsInLayout();
            Log.i(TAG, "Pending Views " +  tableLayout1.getChildCount());
        }

        //Create Table Layout and set its default attributes

        tableLayout1.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));

        //add Tablelayout to HScroll
        sumtablelay.addView(tableLayout1);

        //Create Table row and set its default attribute
        TableRow tableRow1 = new TableRow(this);
        tableRow1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        //tableRow1.setBackground(new ColorDrawable(tablecolor1));
        //add row to tablelayout
       Log.i(TAG,"Views at Rows "+tableRow1.getChildCount());
       tableRow1.removeAllViews();
        Log.i(TAG,"Pending Views at Rows "+tableRow1.getChildCount());

        tableLayout1.addView(tableRow1);

        //add headings in table row
        textView = new TextView(this);
        textView.setText("Date");
        textView.setPadding(30, 2, 2, 2);
        textView.setAllCaps(true);
        // textView.setTextColor(textcolorpri);
        textView.setTextSize(15);
        textView.setGravity(Gravity.CENTER_HORIZONTAL);
        tableRow1.addView(textView);

        textView = new TextView(this);
        textView.setText("Category");
        textView.setPadding(20, 2, 2, 2);
        textView.setAllCaps(true);
        //  textView.setTextColor(textcolorpri);
        textView.setTextSize(15);
        textView.setGravity(Gravity.CENTER_HORIZONTAL);
        tableRow1.addView(textView);



        tableRow1.addView(textView);


//Get Data from Table

        Cursor getexpdata = databaseWorking.getalldatatswithcons(PerformanceTweaks.expdata, "pName", pname);
        while (getexpdata != null && getexpdata.moveToNext()) {
            DataEncap DataEncap = new DataEncap();
            DataEncap.setExpddate(getexpdata.getString(getexpdata.getColumnIndex("dated")));
            DataEncap.setExpcat(getexpdata.getString(getexpdata.getColumnIndex("cate")));

                       tempedata.add(DataEncap);
        }



//Adding Data to above layout table.

        for (int a = 0; a < tempedata.size(); a++) {



            tableRow = new TableRow(this);
            tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

            textView = new TextView(this);
            textView.setGravity(Gravity.CENTER_HORIZONTAL);
            textView.setText(tempedata.get(a).getExpddate());
            //   textView.setWidth(col1);
            textView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
            // textView.setTextColor(textcolorpri);
            textView.setPadding(10, 0, 0, 0);
            tableRow.addView(textView);

            textView = new TextView(this);
            textView.setGravity(Gravity.CENTER_HORIZONTAL);
            textView.setText(tempedata.get(a).getExpcat());
            //   textView.setWidth(col1);
            textView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
            // textView.setTextColor(textcolorpri);
            textView.setPadding(10, 0, 0, 0);
            tableRow.addView(textView);





                tableLayout1.addView(tableRow, new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
                    }
        tablelay.setVisibility(View.VISIBLE);
    }

您可以删除所有使用以下方法的视图 sumtablelay.removeAllViews();