1

I am currently working on an android app where I select a customer in an activity, it retrieves all records pertaining to that customer and displays them in a table layout in a new activity. The problem arises when I go back to the previous activity and select a different customer, the new records are added behind the records of the previous customer (meaning the previous table layout entries are not deleted when I press the back button and go to select a new customer). I've tried 2 solutions:

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

and

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));

Both these solutions do not work for me as the new data is still being added behind the existing data. Could anyone suggest me a different solution that could work?

Also I am attaching the entire code.

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>

Implementing @ Mr.Neo 's solution:

This is how I implemented your solution in my code:

        addHeaders();

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

                addData();
            }
        }, 2000);

But there is still no effect on the TableLayout

Screenshots: 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

  • It's solution is here [stackoverflow solution](http://stackoverflow.com/questions/7302702/how-to-clear-all-rows-from-a-tablelayout) I hope it can help you. – alican akyol Oct 07 '15 at 08:10
  • I used to have this problem and i still can not find the best solution :(. As my way, when I update table layout, I save the old position of `TableLayout`, then I calculate the new position by old position. If you still can not find the solution, I suggest that you use GridLayout instead of TableLayout. It will easier and better – Linh Oct 07 '15 at 08:16
  • while using GridLayout, did it pose any such errors?! @PhanVănLinh – Ashwin Ramamoorthy Oct 07 '15 at 10:22
  • @AshwinRamamoorthy gridview will not have this problem – Linh Oct 08 '15 at 01:27
  • @PhanVănLinh I suggest you don't declare your arrays as `static` and try it once...should work fine along with @uttami solution – Ashwin Ramamoorthy Oct 08 '15 at 08:37
  • @AshwinRamamoorthy thank you. I will try it later – Linh Oct 08 '15 at 08:39

2 Answers2

2

Just try using tl.removeAllViews() instead of tl.removeAllViewsInLayout(). Call this method before adding views in tablelayout.

  • I've tried that too actually...didn't really show any difference – Ashwin Ramamoorthy Oct 07 '15 at 10:24
  • I tried calling `tl.removeAllViews()` method right before calling the 2 methods `addHeaders()` and `addData()` ...did not clear the TableLayout even then – Ashwin Ramamoorthy Oct 07 '15 at 11:15
  • do you see any difference in implementation in my code compared to yours?! or could you share your code so that I can just go through your way of implementation?! – Ashwin Ramamoorthy Oct 07 '15 at 11:36
  • sure...please let me know!! – Ashwin Ramamoorthy Oct 07 '15 at 13:09
  • can you give me your both array details?? as i have implemented your code and im not facing the problem you mentioned (Luckily) . – SinghIsBling Oct 08 '15 at 06:53
  • I'm actually querying data from a sql server into an `ArrayList` and then putting it into the views...but the above mentioned problem does not occur when i provide hard-coded values like `public static ArrayList arr = new ArrayList(Arrays.asList("abc","abc"));` – Ashwin Ramamoorthy Oct 08 '15 at 07:37
  • 1
    I figured it out actually...i just removed the `static` keyword...and it started working fine...and i know I'm not supposed to say thanks here, but thanks for working on my code!! – Ashwin Ramamoorthy Oct 08 '15 at 08:33
  • at last when everything was working i wondered about array keep adding data on back because "Title Row never get repeated" . it was the data part repeating .. well good job u did it !! – SinghIsBling Oct 08 '15 at 10:55
  • 1
    yeah...I guess the `ArrayList` values never got removed from the stack coz it was declared `static` – Ashwin Ramamoorthy Oct 08 '15 at 11:56
0

Here is the solution I am using. Using removeAllViews() and add view after seconds to completely removing. If you add immediately, some rows could not be removed. I know this is not the best solution, but it's the best for me now.

tableMainContent.removeAllViews();
new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        //Add view for table layout here
    }
}, 2000);
Neo
  • 1,469
  • 3
  • 23
  • 40