How do I add view with id in android dynamically?
Hello everyone i'm new to android and I would like to ask about a basic feature.
Suppose I have a TableLayout and I want to add some rows to it dynamically (from java code) so that each row will contain a checkbox that should has a unique ID (suppose row1, row2, etc..). I can't touch r.id at runtime, and the id I pick for the elements must be an integer, so how can I assure it's unique ? is th开发者_StackOverflowere any way to add ID in form of a string (such as row1)?
Thanks in advance, Christopher
Not sure if it would help in your particular case, but you can generate dynamic ids at compile time by using a values.xml file. There you can define items with id type, and the resulting ids would be unique. However, if you have a dynamic number of limits then it may not work for you. If so, consider identifying views by tag rather than by id.
You can use method addView of the TableLayout class : http://developer.android.com/reference/android/widget/TableLayout.html#addView(android.view.View)
This method takes a View object in parameter and as this object's type View, so you can use method setId: http://developer.android.com/reference/android/view/View.html#setId(int)
All ui elements extends of the view class.
Bye.
精彩评论