R.string.value 帮助 android 通知

发布于 2024-09-12 11:39:38 字数 256 浏览 9 评论 0原文

是怎么回事

 CharSequence contentTitle = R.string.value;

错误无法从 int 转换为 CharSequence 。有办法解决这个问题还是我错过了什么? 我尝试过

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

它返回整数值。 有什么帮助吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

迷荒 2024-09-19 11:39:38

R.string.value 是对 R 类中静态字段的调用,该字段由 Eclipse 自动生成,并对所有资源进行某种汇总。要检索字符串,您需要使用:

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

如果打开 R 类,您将看到它仅包含对项目已编译资源的引用的数字。

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.

分開簡單 2024-09-19 11:39:38

要检索字符串,您需要使用 getString(),

但 getString() 是 Context 类中的方法。
如果您想在 Activity 类之外使用此方法,您应该首先获取到上下文的链接,然后调用:

String s = mContext.getString(R.string.somestring)

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)
傻比既视感 2024-09-19 11:39:38

R.string.value 返回资源“value”的引用 ID 号。如果您查看 R 类,它将显示为如下所示:

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

我在引用 getString() 方法时遇到了问题。 Eclipse 向我吐出的确切错误是:

DatabaseHelper.MainDatabaseHelper 类型的 getString(int) 方法未定义

阅读了一段时间后,我发现您必须引用应用程序的上下文才能访问 getString() 方法。我试图在内容提供程序中创建一个私有 SQLDatabase 帮助程序类,但是,这不允许我引用 getString() 方法。到目前为止,我的解决方案是执行以下操作:

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));
    }
}

注意这两个上下文引用:

上下文.getString()

(getContext()).getString()

我不知道这是否是最佳的长期解决方案,但目前看来可行。希望这有帮助。

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.

猥琐帝 2024-09-19 11:39:38

您也可以使用 String s = getResources().getString(R.string.value); 。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文