【问题标题】:Clearing a Table Layout dynamically动态清除表格布局
【发布时间】:2015-10-07 18:48:57
【问题描述】:

我目前正在开发一个 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

【问题讨论】:

  • 解决方法在这里stackoverflow solution希望能帮到你。
  • 我曾经遇到过这个问题,但我仍然找不到最佳解决方案:(。作为我的方式,当我更新表格布局时,我保存了TableLayout 的旧位置,然后我计算旧位置新位置,如果还是找不到解决办法,建议你用GridLayout代替TableLayout,这样会更简单更好
  • 在使用GridLayout的时候,有没有出现这样的错误?! @PhanVănLinh
  • @AshwinRamamoorthy gridview 不会有这个问题
  • @PhanVănLinh 我建议您不要将您的数组声明为static 并尝试一次...应该与@uttami 解决方案一起正常工作

标签: android android-tablelayout


【解决方案1】:

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

【讨论】:

  • 其实我也试过了……没啥区别
  • 我尝试在调用addHeaders()addData() 这两个方法之前调用tl.removeAllViews() 方法...即使那时也没有清除TableLayout
  • 您是否发现我的代码与您的代码在实现上有什么不同?!或者你能分享你的代码,这样我就可以通过你的实现方式?!
  • 实际上我想通了...我刚刚删除了 static 关键字...它开始正常工作...我知道我不应该在这里说谢谢,但是谢谢用于处理我的代码!
  • 是的...我猜ArrayList 值从未从堆栈中删除,因为它被声明为static
【解决方案2】:

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

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

【讨论】:

  • 虽然这个解决方案对我不起作用...没有一行被删除
  • ?你能发布你的结果吗?
  • 我已经编辑了我的初始帖子并添加了您发布的解决方案和结果
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-13
  • 2015-06-02
  • 2021-12-22
  • 1970-01-01
  • 2012-01-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多