1

I want to give ids to multiple views created programmatically and each id to be a specific integer based on my own logic. Consider the following example:

  1. I have M colors defined in a sequential way, e.g. using enum, or using an array to put M color values. (Which implementation/structure to use is part of the puzzle and suggestions need to be provided for this as well, in order to achieve the final goal described below.)
  2. Create N instances of a custom Class and store them in a sequential way too (e.g. an array of N elements). This custom Class will just have a member mColor where a color for the specific instance will be stored.
  3. Create (programmatically always) N RadioGroups with M RadioButtons for each RadioGroup.
  4. The goal is when user clicks the j-th RadioButton of the i-th RadioGroup, then the i-th instance will use as background color the j-th color.

So, I would like to do something like this when OnCheckedChanged event occurs for a RadioGroup:

public void onCheckedChanged(RadioGroup group, int checkedId) {
    int i = group.getId();
    /*j variable is not actually needed here, but is used for concistency
    with the description above.*/
    int j = checkedId;

    /*assume for simplicity that an array of N elements is used to store
    the N CustomCLass instances and an array of M elements is used for 
    colors*/
    customClassArray[i].setmColor(colorsArray[j]);
} 

From the above, I think it is necessary that specific ids for the programmatically created RadioGroups and RadioButtons need to be set. If there is a way for this to be done, then code will be clear, optimized and no switch statements will be needed for N RadioGroups and M RadioButtons (for which, in any case, I have no clue about how it could be implemented too).

Which is the best way to achieve this, firstly with efficiency in mind and secondly, with clear code in mind?

Thank you in advance for any suggestions.

Edit: For clarification, I have read about setting my own ids by using View.setId(), but there are many resources on the web which are against this hardcoded approach (and I also vote against this if there is another way) and many of them suggest using xml resource to put your ids. However, as I said earlier, I think this is not what I want in order to achieve my goal. Example links suggesting this approach: https://code.google.com/p/android/issues/detail?id=75081 or also How to set Id of dynamic created layout?. Also, this approach is the suggested one in the android developer documentation: http://developer.android.com/guide/topics/resources/more-resources.html#Id

Lastly, I have also read this: What is the main purpose of setTag() getTag() methods of View? which explains the purpose of Tags and the use of setTag()/getTag() methods. This may be the best approach I've found so far for what I need. However, it surely introduces more complicated, unsupportable code. In any case, the right way, I think, has to be connected with ids, whose purpose is to uniquely identify views, rather than tags.

Community
  • 1
  • 1
  • There are N*M RadioButtons , but N colors..? – natario Sep 27 '15 at 14:12
  • That's right. You have N RadioGroups and N Instances of my custom class. So, when the i-th RadioGroup receives an `OnCheckedChanged` event , you take the i-th Instance. And then, you find which one of the M RadioButtons that are included in the i-th RadioGroup was just clicked. Let's say it is the j-th RadioButton. So, finally you set the mColor (of the i-th Instance) to the j-th color. – panagspirou Sep 27 '15 at 14:47
  • How big are these numbers? – natario Sep 27 '15 at 15:36
  • Actually, because I don't want a static solution but a dynamic one, I'll tell you that the numbers are unspecified and user specifies them. Ok, both are smaller than 50 for example, if this suffices for some reason to you. – panagspirou Oct 02 '15 at 10:07

1 Answers1

0

As you are creating RadioGroups and RadioButtons from java, I suggest to create a map and use radiobutton reference as key and instance-color combination as value. assign a single checkChangedListener to all your radioButtons and process the event as follows

    @Override
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

          colorInstanceCombo = map.get(buttonView);
          //use colorInstanceCombo to do your work    
     }

assigning ids dynamically can cause problems as id is an integer and your assigned ids may cause conflict with system assigned ids to other components.

Rahul Tiwari
  • 6,851
  • 3
  • 49
  • 78
  • It's ok to hold their references. But when `OnClick` occurs, how will I find to which RadioGroup and which RadioButton the event corresponds to? I do have to search among RadioGroups/RadioButtons - maybe with the classical switch statement - to find e.g. that R.id.radiogroup1_btn2 was clicked. And after that, to go to the array[1][2]... Am I missing or misundertand something? – panagspirou Sep 27 '15 at 13:50
  • in this case you should be going with `HashMap`. use references to your radio buttons as keys and specific object to your custom class as value. using this you can quickly get value for any radio button checked. you can also use this approach with tags instead of direct references as you updated in your question. – Rahul Tiwari Sep 27 '15 at 13:57
  • `RadioGroups` correspond to different Instances and Colors correspond to different `RadioButtons`, so your `HashMap` suggestion would be between `RadioGroups` references and Instances of my custom class. I got your point. However, I think that the problem of identifying **quickly** (not through multiple switch statements etc) the pair of RadioGroup-RadioButton, when `onCheckedChanged` event occurs (not `OnClick` that I said to the previous post by mistake), remains. – panagspirou Sep 27 '15 at 14:30
  • If I've got something wrong, you may provide an example based on the `OnCheckedChanged` event to explain me your logic in more detail. – panagspirou Sep 27 '15 at 14:37
  • @enthusiast check updated answer. as every radiobutton represent a unique instance-color combo you can hold that in a custom object and put it in your map. – Rahul Tiwari Sep 27 '15 at 15:03
  • Yes, that is a solution, you're right, and also you answered to my initial question about setting integer ids to views, so I'll accept it as an aswer. I don't know, however, it just seems a bit overkill to me for some reason, meaning that I'd supposed that there might be a simpler solution, related with just giving your own prefferred integer values as identifiers to each view dynamically. So, any more ideas and opinions about giving my own integer identifier (using id atrribute or whatever else) to a number of views, are welcomed and appreciated! – panagspirou Oct 02 '15 at 09:58
  • @enthusiast as you already have explored about tags. you can combine this approach with tags as well. – Rahul Tiwari Oct 02 '15 at 10:02
  • Yeah that's true! Thanks for the confirmation about that. – panagspirou Oct 02 '15 at 10:12