Android getString方法
我正在尝试将设置为Android getString方法,android,string,Android,String,我正在尝试将设置为String.xml的字符串值与使用以下代码的日期连接起来: holder.time.setText(date.getHours()+" " + getString(R.string.ore)); holder.time.setText(date.getHours()+" " +R.string.ore); 但我收到错误:无法解决方法。 我已尝试使用此代码: holder.time.setText(date.getHours()+" " + getString(R.stri
String.xml
的字符串值与使用以下代码的日期连接起来:
holder.time.setText(date.getHours()+" " + getString(R.string.ore));
holder.time.setText(date.getHours()+" " +R.string.ore);
但我收到错误:无法解决方法。
我已尝试使用此代码:
holder.time.setText(date.getHours()+" " + getString(R.string.ore));
holder.time.setText(date.getHours()+" " +R.string.ore);
可以工作,但值R.string.ore
作为int值而不是字符串检索,但我需要一个字符串值
我需要使用string.xml中的值,而不是使用简单的字符串示例“ore”
有什么帮助吗?如果您在活动的上下文中,请使用以下命令:
getResources().getString(R.string.ore);
getActivity().getResources().getString(R.string.ore);
如果您在片段的上下文中,请使用以下命令:
getResources().getString(R.string.ore);
getActivity().getResources().getString(R.string.ore);
如果您在适配器中
,则需要通过适配器的构造函数将容器活动的上下文传递给它
// When creating the adapter, pass the activity's context
// keeping in mind the notes above
YourAdapter adapter = new YourAdapter(this, ...);
并从适配器内部执行以下操作:
private Context mContext; // A member variable of the adapter class
public YourAdapter(Context context, ...) {
mContext = context;
}
然后可以通过以下方式获取字符串资源:
mContext.getResources().getString(R.string.ore);
如果您处于活动的上下文中,请使用以下命令:
getResources().getString(R.string.ore);
getActivity().getResources().getString(R.string.ore);
如果您在片段的上下文中,请使用以下命令:
getResources().getString(R.string.ore);
getActivity().getResources().getString(R.string.ore);
如果您在适配器中
,则需要通过适配器的构造函数将容器活动的上下文传递给它
// When creating the adapter, pass the activity's context
// keeping in mind the notes above
YourAdapter adapter = new YourAdapter(this, ...);
并从适配器内部执行以下操作:
private Context mContext; // A member variable of the adapter class
public YourAdapter(Context context, ...) {
mContext = context;
}
然后可以通过以下方式获取字符串资源:
mContext.getResources().getString(R.string.ore);
getString
是Context
的一种方法。如果无法解决它,则表示您的类不是上下文
的子类。你可以用
holder.time.getContext()
要检索上下文
,您需要将文本视图
充气并访问getString()
。例如holder.time.getContext().getString(R.string.hour)
getString
是Context
的一种方法。如果无法解决它,则表示您的类不是上下文
的子类。你可以用
holder.time.getContext()
要检索上下文
,您需要将文本视图
充气并访问getString()
。例如holder.time.getContext().getString(R.string.hour)
试试这个
Calendar calendar = Calendar.getInstance();
holder.time.setText(calendar.get(Calendar.HOUR)+" " +getString(R.string.ore));
这可能对你有帮助。试试这个
Calendar calendar = Calendar.getInstance();
holder.time.setText(calendar.get(Calendar.HOUR)+" " +getString(R.string.ore));
这可能会对您有所帮助。尝试如下:getResources().getString(R.string.ore)
这应该可以在任何地方使用:holder.time.getContext().getString(R.string.ore)
尝试如下:getResources().getString(R.string.ore)
这应该可以在任何地方使用:holder.time.getContext().getString(R.ore)
。看起来他在适配器中
@JaredBurrows你说得对,我会更新我的答案。如果你正在使用适配器,你应该已经有权访问上下文
@MohammedAoufZOUAG你好,你能看看这个答案并告诉我如果你知道发生了什么,请告诉我他好像在适配器中
@JaredBurrows你是对的,我会更新我的答案。如果你正在使用适配器,您应该已经有权访问上下文
@MohammedAoufZOUAG您好您能看看这个答案并告诉我您是否知道发生了什么吗