Ok so something that I am confused with is whether Android ids need to be unique or not. Here is why the confusion arises:
Let's just say there is an Activity
that has an TextView
(android:id="text") and a Button
(android:id="button"). The Button
sets the text of the text view to a random text. So, to add a listener, I will retrieve the button as
Button b = (Button) findViewById(R.id.button)
and then add listener to do the task.
Now, I can refer to this same TextView from a DialogFragment
, a Fragment
and what not with the same id R.id.text
and all the changes will ever be applied to this text view without doubt.
In case of subclassing BaseAdapter
, you need to override the getView
where you do the inflating if necessary and if not, you make changes. Now, you retrieve the Views
in almost the same way. Almost.
You do a convertView.findViewById(..)
.
If all the views need to have a unique id, how does changing the content of View
in the getView
not result in haphazard behavior ?
I mean, all the views inflated have the same id as defined in layout.
My understanding is that the ids are unique in the context of the view that inflated them. Since I am a beginner, I am asking for clarification
Update after Ahmad's answer
So, what this means is I can not inflate two Fragment
in an Activity
by using the same layout. This will result in an exception. Both are inflated by the same Activity
, hence belong to the same instance. The ids will conflict