动态清除 Table 布局

Clearing a Table Layout dynamically

我目前正在开发一个 android 应用程序,我 select 一个客户在 activity 中,它检索与该客户有关的所有记录并将它们显示在 table 新布局 activity。 当我回到之前的 activity 和 select 一个不同的客户时,问题就出现了,新记录被添加到之前客户的记录后面(意味着之前的 table 布局条目不是当我按下后退按钮并转到 select 一个新客户时删除)。 我尝试了 2 种解决方案:

tl = (TableLayout) findViewById(R.id.maintable);
tl.removeAllViewsInLayout();

layout = (RelativeLayout)findViewById(R.id.layout1);
tl = (TableLayout) findViewById(R.id.maintable);
int count=tl.getChildCount();
for(int i=0;i<count;i++)
tl.removeView(layout.getChildAt(i));

这两种解决方案都不适合我,因为新数据仍在添加到现有数据之后。 谁能给我推荐一个可行的不同解决方案?

另外我附上了完整的代码。

OutstandingBills.java:

@Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_outstanding_bills);
        layout = (RelativeLayout)findViewById(R.id.layout1);
        tl = (TableLayout) findViewById(R.id.maintable);
        tl.removeAllViewsInLayout();
        int count=tl.getChildCount();
        for(int i=0;i<count;i++)
            tl.removeView(layout.getChildAt(i));
        addHeaders();
        addData();
    }

    public void addHeaders(){

        tr = new TableRow(OutstandingBills.this);
        tr.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        TextView bilDateTV = new TextView(OutstandingBills.this);
        bilDateTV.setText("Date");
        bilDateTV.setTextColor(Color.BLACK);
        bilDateTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
        bilDateTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        bilDateTV.setPadding(5, 5, 5, 0);
        tr.addView(bilDateTV);  // Adding textView to tablerow.

        TextView billNoTV = new TextView(OutstandingBills.this);
        billNoTV.setText("Bill No.");
        billNoTV.setTextColor(Color.BLACK);
        billNoTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        billNoTV.setPadding(5, 5, 5, 0);
        billNoTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
        tr.addView(billNoTV); // Adding textView to tablerow.

        TextView dueAmtTV = new TextView(OutstandingBills.this);
        dueAmtTV.setText("Amount Due");
        dueAmtTV.setTextColor(Color.BLACK);
        dueAmtTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        dueAmtTV.setPadding(5, 5, 5, 0);
        dueAmtTV.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
        tr.addView(dueAmtTV); // Adding textView to tablerow.


        tl.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        tr = new TableRow(OutstandingBills.this);
        tr.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        TextView divider = new TextView(OutstandingBills.this);
        divider.setText("-----------------");
        divider.setTextColor(Color.BLACK);
        divider.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        divider.setPadding(5, 0, 0, 0);
        divider.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
        tr.addView(divider); // Adding textView to tablerow.

        TextView divider2 = new TextView(OutstandingBills.this);
        divider2.setText("-------------------------");
        divider2.setTextColor(Color.BLACK);
        divider2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        divider2.setPadding(5, 0, 0, 0);
        divider2.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
        tr.addView(divider2); // Adding textView to tablerow.

        TextView divider3 = new TextView(OutstandingBills.this);
        divider3.setText("-------------------------");
        divider3.setTextColor(Color.BLACK);
        divider3.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        divider3.setPadding(5, 0, 0, 0);
        divider3.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
        tr.addView(divider3); // Adding textView to tablerow.

        // Add the TableRow to the TableLayout
        tl.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
    }

    public void addData(){

        for (int i = 0; i < arr.size(); i++)
        {
            tr = new TableRow(OutstandingBills.this);
            tr.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));

            bilDate = new TextView(OutstandingBills.this);
            bilDate.setText(arr.get(i).toString());
            bilDate.setTextColor(Color.BLACK);
            bilDate.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
            bilDate.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            bilDate.setPadding(5, 5, 5, 5);
            tr.addView(bilDate);  // Adding textView to tablerow.

            bilNo = new TextView(OutstandingBills.this);
            bilNo.setText(arr1.get(i).toString());
            bilNo.setTextColor(Color.BLACK);
            bilNo.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            bilNo.setPadding(5, 5, 5, 5);
            bilNo.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
            tr.addView(bilNo); // Adding textView to tablerow.

            dueAmt = new TextView(OutstandingBills.this);
            dueAmt.setText(arr2.get(i).toString());
            dueAmt.setTextColor(Color.BLACK);
            dueAmt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
            dueAmt.setPadding(5, 5, 5, 5);
            dueAmt.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
            tr.addView(dueAmt); // Adding textView to tablerow.

            // Add the TableRow to the TableLayout
            tl.addView(tr, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
        }
    }

OutstandingBills.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:id="@+id/layout1"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.ashwin.projectx1.OutstandingBills">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="none">
        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:stretchColumns="0,1"
            android:id="@+id/maintable" >
        </TableLayout>
    </ScrollView>

</RelativeLayout>

实施@Mr.Neo的解决方案:

这就是我在代码中实现您的解决方案的方式:

        addHeaders();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                //Add view for table layout here

                addData();
            }
        }, 2000);

但是对TableLayout还是没有影响

截图: The first time I enter the activity with a Customer name

On re-entering the activity the second time, you can see the duplicate records

只需尝试使用 tl.removeAllViews() 而不是 tl.removeAllViewsInLayout()。 在 tablelayout 中添加视图之前调用此方法。

这是我正在使用的解决方案。使用 removeAllViews() 并在几秒钟后添加视图以完全删除。如果立即添加,则无法删除某些行。我知道这不是最好的解决方案,但它现在对我来说是最好的。

tableMainContent.removeAllViews();
new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        //Add view for table layout here
    }
}, 2000);