我目前正在使用一个android应用程序,在其中选择一个活动中的客户,它会检索与该客户有关的所有记录,并将它们显示在新活动中的表格布局中。
当我返回上一个活动并选择另一个客户时,就会出现问题,新记录会添加到前一个客户的记录后面(这意味着当我按下“后退”按钮并选择一个新客户)。
我尝试了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()。
在表布局中添加视图之前,请调用此方法。

10-07 19:33
查看更多