R.string.value Help android notification
Asked Answered
N

4

22

whats the deal with

 CharSequence contentTitle = R.string.value;

Error cannot convert from int to CharSequence. Is there a way around this or am i missing something? i tried

String s = R.string.value + "";
CharSequence contentTitle = s;

it returns integers values. Any help?

Nurse answered 29/7, 2010 at 13:59 Comment(0)
T
55

R.string.value is a call to the static field in the class R, which is auto generated by Eclipse and which does a kind of summary of all your resources. To retrieve the string, you need to use :

CharSequence contentTitle = getString(R.string.value);

If you open the R class you will see that it contains only numbers that are references to the compiled resources of your project.

Tertial answered 29/7, 2010 at 14:2 Comment(7)
@Tertial I'm having an issue with getString(), I keep getting "The method getString(int) is undefined..." what am I doing wrong?Secretary
What do try getting with this method?Tertial
@Tertial I'm just trying to retrieve the actual value stored in strings.xmlSecretary
This is quite weird, I can't answer you just like that, I would need to see your code. Maybe you are doing something wrong. Do you know that this method is either associated to a context instance OR a Resource instance. So, maybe this is your issue.Tertial
Perhaps this would help solve he problem? => context.getString(R.string.value)Tribadism
Indeed, that's what I was suggesting. Thank you for the precisionTertial
You could also try getResources().getString(R.string.value)Nomen
T
7

To retrieve the string, you need to use getString(),

but getString() is a method from Context class. If you want to use this method outside your Activity class, you should get link to your context first and then call:

String s = mContext.getString(R.string.somestring)
Thaw answered 28/12, 2011 at 12:18 Comment(0)
C
1

R.string.value returns the reference ID number of the resource 'value'. If you look at your R class it will appear as something like this:

public static final class string {
  public static final int value=0x7f040007;
}

I've been experiencing issues with referencing the getString() method. The exact error that Eclipse spits at me is:

The method getString(int) is undefined for the type DatabaseHelper.MainDatabaseHelper

After reading for awhile I've figured out that you must reference your application's context to get access to the getString() method. I was trying to create a private SQLDatabase helper class in a content provider, however, that was not allowing me to reference the getString() method. My solution so far is to do something like this:

private class MainDatabaseHelper extends SQLiteOpenHelper {

    MainDatabaseHelper(Context context) {
        super(context, context.getString(R.string.createRoutesTable), null, 1);
    }

    public void onCreate(SQLiteDatabase db) {
        db.execSQL((getContext()).getString(R.string.createRoutesTable));
    }
}

Notice these two context references:

context.getString()

(getContext()).getString()

I don't know if this is the optimal long-term solution but it seems to work for the moment. Hope this helps.

Canasta answered 4/4, 2012 at 2:38 Comment(0)
P
0

You could use String s = getResources().getString(R.string.value); also.

Pursuance answered 30/10, 2013 at 12:12 Comment(1)
How to get access in some java class (not activity) such as viewPagerAdapter. I don't have there 'getContext'?Used

© 2022 - 2024 — McMap. All rights reserved.