Use "findViewById" repetitively or store View?

975 views Asked by At

Being the efficiency freak that I am (as I'm sure lots of you all are as well), I've wondered this for a while and just thought of posing this question:

Two scenarios, possibly two different answers.

If I'm looping through a list of items and updating a bunch of EditTexts in a LinearLayout as such, what are the pros/cons of these two methods: 1)

for (int i = 0; i < itemList.size(); i++) {
    ((TextView)findViewById(itemList.get(i).getId())).setText(itemList.getText());
    ((TextView)findViewById(itemList.get(i).getId())).setColor(itemList.getColor());
}

2)

TextView tv;
for (int i = 0; i < itemList.size(); i++) {
    tv = ((TextView)findViewById(itemList.get(i).getId()));
    tv.setText(itemList.getText());
    tv.setColor(itemList.getColor());
}

I think the underlying question is how efficient is "findViewById"? This may be picky, I think 2) is the better solution. Thanks!

7

There are 7 answers

2
Tim On BEST ANSWER

This is not picky at all. 2nd option is without doubt the better one.

1

for (int i = 0; i < itemList.size(); i++) {
    ((TextView)findViewById(itemList.get(i).getId())).setText(itemList.getText());
    ((TextView)findViewById(itemList.get(i).getId())).setColor(itemList.getColor());
}

Looks clean, but isn't. If you are working with one and the same textview, absolutely do not call findViewById more than once.

2

TextView tv;
for (int i = 0; i < itemList.size(); i++) {
    tv = ((TextView)findViewById(itemList.get(i).getId()));
    tv.setText(itemList.getText());
    tv.setColor(itemList.getColor());
}

This is the better option, because it only calls findViewById once. It's a little less readable, though.

You could also consider a 3rd option

for (int i = 0; i < itemList.size(); i++) {
    TextView tv = ((TextView)findViewById(itemList.get(i).getId()));
    tv.setText(itemList.getText());
    tv.setColor(itemList.getColor());
}

This keeps everything in the loop (easier to read, imo) without notably sacrificing efficiency. I prefer the 3rd, but the 2nd is a good pick as well.

0
Thomas Kaliakos On

I think without doubt the second option is better.
Not only you save the cost of calling findViewById one extra time (ok at the cost of one extra local variable)
but the code is also much more readable.

0
Simas On

A google employee Dianne Hackborn has answered a very similar question here.

She says that you should avoid using findViewByid repetitevely whenever you can.

0
AudioBubble On

You should use new RecyclerView if possible now. Combined with LinearLayoutManager it'll allow you achieve the same, but you'll be forced to use ViewHolder pattern. If you go with your ListView, you should also implement ViewHolder. findViewById is definitely not efficient, so you need to prevent too many calls to it.

0
Rami On

Second way is better, because the cost of findViewById() is acceptable in static UI layouts. However, since getView() is called frequently, the usage of findViewById() should be kept to minimum.

0
ChatterOne On

With your second option you save: - A call to findViewById() - A call to itemList.get(i) - A call to [itemList.get(i)] getId()

Also, note that in a for loop usually going backward is a little bit faster (more optimized) than going forward (because i < value translates to i-value < 0, which is more expensive than i > 0).

1
Abdullah Alhomidi On

The second is for sure less expensive by 50%. But I would prefer @TimCastelijns 3rd method because he is dumping the view reference at the end of the loop.

In the first method, you use findViewById twice. In the second method, you use it once and save a reference to it, which saves 50% off of the resource usage.

I preferred @TimCastelijns, because he saves it as a local variable, which will be dumped, therefore saving resources.