0

Solved with this answer How can I get a resource content from a static context?

Today I decided that my app should be available in different languages so I changed every normal string like...:

String hw = "hello world";

..to:

XML:

<string name="hello_world">Hello world!</string>
App:

String hw= getResources().getString(R.string.hello_world);

But for some reason it doesn't show the text anymore. Everthing worked fine when I used the normal methode but now it doesn't. I tried getText() too but this doesn't work neither. I am using the strings to get them into an ArrayList like this:

public  hello(){

    meineListe.add(hello_world);

Complete class:

public class favorites extends Activity {

static int x = -1;

private ArrayList<String> meineListe = new ArrayList<String>();

public  favorites(){
    String a1 = getResources().getString(R.string.a1);
    String aa1 = getResources().getString(R.string.aa1);
    String aaa1 = getResources().getString(R.string.aaa1);
Community
  • 1
  • 1
Silas
  • 21
  • 4

2 Answers2

1

Try - meineListe.add(getResources().getString(R.string.hello_world)); and make sure that you declared strings in res/values/strings.xml file.

NehaK
  • 2,639
  • 1
  • 15
  • 31
  • tried it but doesn't work too – Silas Jun 12 '15 at 20:46
  • This is what the OP is doing already. Just trying to add it to a String variable before adding to the list – codeMagic Jun 12 '15 at 20:50
  • @Silas i edited my code recheck it. – NehaK Jun 12 '15 at 21:13
  • Still doesnt work unfortunately – Silas Jun 12 '15 at 21:24
  • @Silas What is problem in code? data is null or any exception? – NehaK Jun 12 '15 at 21:25
  • I am using the array list to change a textview everytime someone presses a button. This worked when I used the strings normal but if i use them like this then i press the button but nothing happens – Silas Jun 12 '15 at 21:51
  • @Silas can you please add your full code to add data in array. – NehaK Jun 12 '15 at 21:54
  • This basically is my full code to add it into the array. There are just more strings but this is how im adding it. – Silas Jun 12 '15 at 22:08
  • @Silas Make sure R is not android.R it should be your_package.R in imports. – NehaK Jun 12 '15 at 22:12
  • still doesn't work. I edited my question to show the complete adding. Do I need to make the getResources().getString(R.string.hello_world) in the MainActivity? Because this isn't my MainActivity I just extended Activity. Does this make any difference and if so is there a way to still use it not in the main acitivity? – Silas Jun 12 '15 at 22:33
  • I actually managed to let it work now. You were a pretty big help and brought me in the right direction. thank you! – Silas Jun 12 '15 at 23:36
0

Ok so,the point is that you need the context of your main activity.

I am not sure where you call that getResources().getString bla bla, but,it goes like this:

CASE 1: You call it in your main activity.

If this is the case,you do this:

 String hw= this.getResources().getString(R.string.hello_world);

CASE2: You call it in some other class/activity/fragment?

Step1: In your main activity you add this code:

 Context context = this;

Step2: You pass the context to whatever class you want and you use it like you used "this" in your main activity.

 Context context;
 //
 //Code,variables,whatever you got there
 //

 //In your constructor, or method which you used to pass the context from main activity:

 context.this = context;
 String hw= context.getResources().getString(R.string.hello_world);

This should do the trick.

Vlad
  • 910
  • 1
  • 10
  • 18