Android - assign and receive identifier dynamically

I know how to dynamically determine an identifier by calling setID() . In order for the identifier to be unique, I used ids.xml and passed the setID() identifier from the pre-generated identifier pool.

Question 1: Is it possible to assign an identifier without using ids.xml , since I cannot predict how many identifiers I will need at runtime?

I tried to get around the first problem presented in Question 1 by dynamically assigning each of them an identifier based on its label hash (each label is unique), but there is no way to guarantee that this ID will not collide with the identifier automatically generated in R.java .

Question 1.1: How can I resolve the name conflict of names?

Question 2: Suppose I have an ID value that I assign and generate dynamically. Since the above identifier does not appear in R.id , findViewById() will not be used to get the view. Therefore, how can I get a view when the identifier is known?

Answer 2:. You can get a view by its corresponding identifier only after onCreate() returns control (completed).

+1
android android-layout android-widget
source share
2 answers

Is there a way to assign an identifier without using ids.xml, since I cannot predict how many identifiers I will need at runtime?

This ensures that each view has a unique identifier.

 for(int i =0 ; i < yourIDcount ; i++){ yourView.setId(i); } 

How can I get a view when the identifier is known?

 View.findViewById(yourView.getId()); 

can be used to get your view identifier, since each view has a unique identifier, you can return the desired view.

The word dynamic means that it is created at run time, since you assign an id to onCreate, it is assigned as the identifier of views, since onCreate is called only once when the action is created, you can make sure that the identifier you have assigned remains unchanged ...

0
source share

From API level 17 you can get a new identifier by calling

 View.generateViewId() 

Details here.

+7
source share

All Articles