0

I'm trying do something like this

public class CytatCore {

    public static void cytatCore(int number, TextView tv) {

       tv.setText(R.string.text+number);
    }
}

I've a lot of string in xml named e.g "text1", "text2" etc. Only last value is changing. I tried to do this in couple ways, but I still get errors in code.

4 Answers 4

3

I think followin code wil work for you

switch(number) {
    case 1 : tv.setText(R.string.text1);
    case 2 : tv.setText(R.string.text2);
}

while using this type code, put also text1, text2 in your R.string; switch case also processed faster.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, I am trying to to avoid this :P That's why I write here :D
2

I'm a bit confused on what you're trying to accomplish because your question is not written clearly, but I'll take a stab in the dark and assume your question is

How do I append a number onto the end of a string I have in XML?

Edit: My assumption was wrong, it appears your question is rather

How do I get a String from XML by name reference?

Using the getIdentifier() method of a Context will look up an ID by name...but be warned that this operation is not recommended if it's used extremely often, as it's slow.

public class CytatCore {

    public static void cytatCore(Context context, int number, TextView tv) {

       int textId = context.getResources().getIdentifier("text" + number, "string", context.getPackageName());
       tv.setText(textId);
    }
}

2 Comments

No, in xml file i've something like this <string name="text1">Some text</string> and <string name="text2">Some text2</string> and i want to choose one, and which it will be selected depends on int number
Thanks you helped a lot :) I had long pause in programming for android.
1

Another option you have is to get a reference to the Resources object and use the method getIdentifier(). If you are in an activity then you could do:

public void cytatCore(int number, TextView tv) {    
    int id = getResources().getIdentifier("text" + 1, "string", this.getPackageName());
    t.setText(id);
}

Comments

0

Try putting the names in an array:

...
private String[] ids = new String[N];
for (int i = 0; i < N; i++) {
    ids[i] = context.getString(R.string.resource_name) + i;
}

and then:

...
public static void cytatCore(int i, TextView tv) {
    tv.setText(ids[i]);
}

or simply:

    ...
public static void cytatCore(int i, TextView tv) {
    tv.setText(context.getString(R.string.resource_name) + i);
}

3 Comments

This wont work because "text" is a variable name not a real string name that you can dynamically change
I know i can do it in that way (or something similar), but I'm looking for the simplest way :)
You have to remove at least the first sample from your post and the last one still could return unexpected strings if you don't put the strings in order..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.