[英]How do I call getString() inside the onBindViewHolder() method of a recycler view?
I am creating a RecyclerAdapter to display the forecast info on a certain day.我正在创建一个 RecyclerAdapter 来显示某一天的预测信息。 My RecyclerView contains multiple days, each of which is modified with the onBindViewHolder.
我的 RecyclerView 包含多天,每一天都用 onBindViewHolder 修改。
The layout of each day has 3 text views.每天的布局有 3 个文本视图。 The first one contains a string that is the summary.
第一个包含作为摘要的字符串。 The second one contains a string with a double as a positional argument which represents the low temperature.
第二个包含一个字符串,其中 double 作为位置参数,表示低温。 The third is identical to the second, but represents the high temperature.
第三个与第二个相同,但代表高温。
Below is the code of my onBindViewHolder method:下面是我的 onBindViewHolder 方法的代码:
@Override
public void onBindViewHolder(@NonNull DailyForecastAdapter.ViewHolder viewHolder, int i) {
Datum datum = forecast.get(i);
TextView summary = viewHolder.summaryTextView;
TextView tempHigh = viewHolder.tempHighTextView;
TextView tempLow = viewHolder.tempLowTextView;
summary.setText(datum.getSummary());
tempHigh.setText(datum.getTemperatureHigh());
tempLow.setText(datum.getTemperatureLow());
}
Since high and low temperatures are doubles , I need to format the string accordingly, lest I overwrite the string with just a double value.由于高温和低温是doubles ,我需要相应地格式化字符串,以免我只用 double 值覆盖字符串。 Here are the string resources for high temperature and low temperature:
以下是高温和低温的字符串资源:
<string name="temperature_high">High of %1$.2f</string>
<string name="temperature_low">Low of %1$.2f</string>
Outside of the RecyclerAdapter class I know how to do this, below is an example of how I format a string inside a Fragment:在 RecyclerAdapter 类之外,我知道如何执行此操作,下面是我如何在 Fragment 中格式化字符串的示例:
String moddedString = String.format(getString(R.string.temperature), temp);
((TextView)activity.findViewById(R.id.temperatureDisplay)).setText(moddedString);
However, I don't have access to the getString()
function inside the RecyclerAdapter , so I cannot format the string appropriately to insert the temperature I need without completely overriding the String with a double.但是,我无法访问 RecyclerAdapter 中的
getString()
函数,因此我无法适当地格式化字符串以插入我需要的温度,而无需使用双精度值完全覆盖字符串。
How do I use getString()
inside the onBindViewHolder()
method?如何在
onBindViewHolder()
方法中使用getString()
?
How do I use getString() inside the onBindViewHolder() method?
如何在 onBindViewHolder() 方法中使用 getString()?
Every ViewHolder
instance has an itemView
field, which is an instance of View
.每个
ViewHolder
实例都有一个itemView
场,这是一个实例View
。 Every View
instance has a getContext()
method;每个
View
实例都有一个getContext()
方法; you can use this to access resources.您可以使用它来访问资源。
String text = viewHolder.itemView.getContext().getString(R.string.mystring);
您可以使用上下文获取字符串资源。
context.getString(R.string.temperature)
You can save a local copy of Context with the constructor of your RecyclerViewAdapter class:您可以使用 RecyclerViewAdapter 类的构造函数保存 Context 的本地副本:
public class YourRecyclerViewAdapter extends RecyclerView.Adapter<YourRecyclerViewAdapter.ViewHolder> {
private Context context;
public YourRecyclerViewAdapter(Context context) {
this.context = context;
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
String string = context.getString(R.string.your_string);
}
//1. Get context from adapter constructor:
public YourRecyclerViewAdapter(Context context)
//2. As @Ben P.said, get context from item view:
Context context = viewHolder.itemView.getContext();
//3. I think the adapter only binds the data to the view
//and doesn’t care about the logic, so maybe you can
//prepare the data before passing it to the adapter
CustomData {
private String temperature;
public String getTemperature() {
return temperature;
}
}
//Then pass the data to adapter by construtor:
YourRecyclerViewAdapter adapter = new YourRecyclerViewAdapter(data);
//Or update data by adapter functions:
adapter.updateData(data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.