动态清除表布局



我目前正在一个android应用程序,我在一个活动中选择一个客户,它检索属于该客户的所有记录,并在一个新的活动表布局中显示它们。当我返回到前一个活动并选择一个不同的客户时,问题就出现了,新记录被添加到前一个客户的记录后面(这意味着当我按下后退按钮并去选择一个新客户时,没有删除前一个表布局条目)。我尝试了两种解决方案:

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

产生影响

截图:第一次输入Customer名称

的活动

第二次重新进入活动时,您可以看到重复的记录

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

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

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

最新更新