2

I'm adding rows to my TableLayout dynamically and each row has more than one TextView.

I want to loop for each row in my TableLayout and I want to update some rows (not every row), for example the text of TextView in my rows. How can I manage this?

1
  • post the code which you have attempted if any? Commented Dec 13, 2011 at 11:24

2 Answers 2

8

Loop through the children of the TableLayout, they should be TableRows (or another layout) - skip the ones you don't want to update. Loop through or findViewById the children of the TableRow (or whatever) which will be your TextViews

When you add the TextView / CheckBox to the TableRow, you can set the View's id with setId(R.id.id_of_the_view) (which you might want to add to the res/values/ids.xml file).

Then loop through like so:

TableLayout tableLayout = null;
for(int n = 0, s = tableLayout.getChildCount(); n < s; ++n) {
    TableRow row = (TableRow)tableLayout.getChildAt(n);
    TextView name = (TextView)row.findViewById(R.id.tv_name);
}

since you're using row.findViewById, you're looking for a specific id inside a specific row.

Sign up to request clarification or add additional context in comments.

5 Comments

Firstly, thank you for your reply. But I don't know how can I loop through the children(my children are TableRows) of TableLayout. Then, another problem is I'm adding TableRows and TextViews, CheckBoxes for each TableRow dynamically. How can I reach the id of specific TextView in a specific TableRow? I'm asking so that you told about "findViewById".
Thank you, I will try it but as far as I seen it's very logical.
I also want to learn how can I loop in ListView like this way? And I want to assign checkbox id from code, like checkbox1, checkbox2, then also, I want to control from 1 to 100 for example if they are checked. How can I do this?
I'm using your code, but there is a problem : tableLayout.getChildCount() returns the multiplication of the number of rows and 2. For example, I have 5 rows and each row has 2 TextViews and 1 CheckBoxes. I could not understand why getChildCount() returns 10. When I get the first child from tableLayout like TableRow row = (TableRow) tableLayout.getChildAt(0) it's working, however getChildAt(1) returns a silly row whose id is -1. For all odd numbers, the id of component returns -1.
The -1 id'ed rows might be the dividers between the rows?
2

I recommend using the setTag method to quick reach specific rows, and Views within rows.

For example, lets say you have a CheckBox in a certain column, which represents "isSomethingEnabled". When creating the CheckBox, do setTag("foo").

Use setTag on the row as well, such as setTag("rowKey")

To quickly get to a specific row

TableRow tRow = (TableRow) tLayout.findViewWithTag("rowKey");

And to quickly get to a specific child

View v = tRow.findViewWithTag("foo");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.