3

I'm using the following code to update an image switcher and corresponding strings when the next button is clicked,but I'm having trouble referencing the strings from the res/strings folder in the GetMyString().

For example one of my strings is named cutString.How do I reference it instead of YOUR_STRING_01? Is there a simple way to do call the string or is there a flaw in this implementation?

 btnNext.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
        // TODO Auto-generated method stub
        currentIndex++;
        // If index reaches maximum reset it
        if(currentIndex==messageCount)
        currentIndex=0;
        imageSwitcher.setImageResource(imageIds[currentIndex]);
        tView.setText(getMyString(clicks++));
        }
        });

        //method called to update textview strings.
        public String getMyString(int variable){
            switch(variable){
                case 1:
                    return YOUR_STRING_01;
                    break;
                case 2:
                    return YOUR_STRING_02;
                    break;
                case 3:
                    return YOUR_STRING_03;
                    break;
            }

6 Answers 6

4

So I notice that your implementation doesnt necessarily have reference to a context so you will need to do something like this.

//method called to update textview strings.
    public String getMyString(final int variable, final Context applicationContext){
        switch(variable){
            case 1:
                return applicationContext.getResources().getString(R.string.something);
                break;
            case 2:
                return applicationContext.getResources().getString(R.string.something2);
                break;
            case 3:
                return applicationContext.getResources().getString(R.string.something3);
                break;
        }

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

1 Comment

Thanks that worked a charm,but I'm still having trouble with where to place the code in the class.I placed the getMyString() method call within the same onClick() for switching the images in the view.But it's giving me errors relating to: ` The method getMyString(int, Context) in the type AboutActivity is not applicable for the arguments (int)`
3

You can access Strings stored in the strings.xml via the getString() function.

Example:

XML file saved at res/values/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello!</string>
</resources>

This layout XML applies a string to a View:

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

This application code retrieves a string:

String string = getString(R.string.hello);

You can use either getString(int) or getText(int) to retrieve a string. getText(int) will retain any rich text styling applied to the string.

Comments

0

use either String str = getResources().getString(R.string.cutString); or String str = getString(R.string.cutString);

both options are part of Context - http://developer.android.com/reference/android/content/Context.html

Comments

0

You reference your string as any other resource, using R class, i.e.:

R.string.cutString;

To get value, use getString():

String text = getResources().getString(R.string.cutString);

Comments

0

Every class that inherits from the Context class has a method called getString, which you can use to retrieve your value.

Assuming that btnNext is inside your activity you just have to call

getString(R.string.cutString)

and the result should be that value of your string.

See http://developer.android.com/reference/android/content/Context.html#getString(int)

Comments

0

Rather than returning a String from getMyString(), why don't you return the resource ID (R.string.cutString) and then use TextView.setText(int resid) to set the text. Essentially, you should just change the return type of getMyString() to int and the return value from the switch statement to something like this:

    public int getMyString(int variable){
        switch(variable){
            case 1:
                return R.string.YOUR_STRING_01;
                break;
            case 2:
                return R.string.YOUR_STRING_02;
                break;
            case 3:
                return R.string.YOUR_STRING_03;
                break;
        }    

Comments

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.