0

Is it possible to change the variable name dynamically. I mean I have 4 textViews with name: textView1, textView2, textView3, textView4

I am adding a count in a for loop I would like do something like this:

for (int i; i<count; i++)
{
  textView+(i).setText(Count)
  //textView1.setText(Count);
  //textView2.setText(Count);
  //textView3.setText(Count);
  //textView4.setText(Count);

  count++
}

How do I achieve this? Is this possible?

Thanks!

TheDevMan
  • 5,914
  • 12
  • 74
  • 144
  • First clear your question. It's not quite clear. – M D Jun 16 '15 at 11:01
  • 1
    You cannot change variable names like that. They are not used after compilation anyway. You have incorrect view of the programming language. check [here](http://stackoverflow.com/questions/6729605/assigning-variables-with-dynamic-names-in-java). You should just use a list of `TextViews` – Kelo Jun 16 '15 at 11:02
  • @MD : instead of textView1 I want it to be textView(i) - i nothing but 1,2,3,4 – TheDevMan Jun 16 '15 at 11:02
  • put the objects in a Collection – Blackbelt Jun 16 '15 at 11:02
  • used `ArrayList with textView`. What's wrong with that? – M D Jun 16 '15 at 11:03
  • create an array containing the resource IDs for your text and loop through them and set the values like you desire – Sree Jun 16 '15 at 11:05
  • that is not possible because I have 10 RelativeLayouts which has heading TexTView and then below each heading I have different imageViews. – TheDevMan Jun 16 '15 at 11:07
  • i dot get your problem in creating array, try to create a int array of your textviews. – Sree Jun 16 '15 at 11:08

3 Answers3

2

It is impossible to change the name of a variable in runtime. Variables are tools you use as a developer to tell the "computer" what you want it to do. This question is as invalid as asking if you can write code that removes the need of adding a semicolon (;) at the end of every statement. Variable names are just names you call certain elements you're working with. This is simply how Java works.

That being said, what you're trying to do can be achieved by using a simple array, and iterating over it. Here's how you can do it:

TextView[] textViews = new TextView[] { textView1, textView2, textView3, textView4 };

for (int i = 0; i < textViews.length; i++)
{
    textViews[i].setText("some text");
} 

Here, you create a new variable called "textViews", which is a TextView type array. Within it, you have 4 references to other TextViews, each defined by the variable name you gave it previously.

EDIT:

If your TextViews are defined in the XML layout file, then you can either initialise the textView1, textView2, .. variables prior to creating the array, or you could initialise the array directly with calls to findViewById, like so:

TextView[] textViews = new TextView[] { 
    (TextView)findViewById(R.id.textView1),
    (TextView)findViewById(R.id.textView2),
    (TextView)findViewById(R.id.textView3),
    (TextView)findViewById(R.id.textView4)
};
Gil Moshayof
  • 16,633
  • 4
  • 47
  • 58
  • But How do I link the textView array to the actual in XML, because I have textView fixed there. – TheDevMan Jun 16 '15 at 11:36
  • Edited my answer to address this question. – Gil Moshayof Jun 16 '15 at 11:39
  • initialization of an array can be made more elegant if you do not use the id and receive dynamic presentation `int id = getResources().getIdentifier("textView" + i, "id", this.getPackageName()); (TextView)findViewById(id)` – SorryForMyEnglish Jun 16 '15 at 11:59
1

basically what you want is not the best practice

but if you can implement much needed it through Reflection

        String textViewName = "textView";
        String methodName = "setText";
        Class activityClass = this.getClass();
        for(int i = 0; i < count; i++) {
            try {
                Field field = activityClass.getField(textViewName + i);
                if(field != null){
                    Method method = field.getClass().getMethod(methodName, String.class);
                    method.invoke(field, Count);
                }
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }
SorryForMyEnglish
  • 1,181
  • 7
  • 14
  • There is no way via reflection to alter the name of a variable within a method. This is only a viable answer if each TextView is a field of the class its contained within, and even then, (to be fair, you did say this), it's a terrible idea to do this. – Gil Moshayof Jun 16 '15 at 11:19
  • where you see that someone changes the variable name? – SorryForMyEnglish Jun 16 '15 at 11:21
  • This is the question the poster asked: "Is it possible to change the variable name dynamically" – Gil Moshayof Jun 16 '15 at 11:23
  • Did you see what he's doing in the code? He iterated by name and change the text in the `TextView` – SorryForMyEnglish Jun 16 '15 at 11:25
  • I think you might have misunderstood the question. The poster wants to be able to change the name of a variable. i.e., if I have a variable named "int a", he's asking if it's possible to change it to "b" during runtime. Such a thing is conceptually possible via reflection, where the variable name is described as a string, but the bigger point here I feel, is that the poster is lacking a fundamental understanding on how Java works. – Gil Moshayof Jun 16 '15 at 11:35
  • but whereas your answer it would help if you did the same thing that I am, once you have added to an array of all the declared variables :-) – SorryForMyEnglish Jun 16 '15 at 11:44
  • Agree to disagree then :) – Gil Moshayof Jun 16 '15 at 11:50
0

Not possible. This is how java works, you can call it syntax rule

Instaed of above you could do

Take a Collection and iterate it .You cant be lazy enough to initialize the variable name at-least !

dharmendra
  • 7,835
  • 5
  • 38
  • 71