【问题标题】:How do I call getString() inside the onBindViewHolder() method of a recycler view?如何在回收站视图的 onBindViewHolder() 方法中调用 getString()?
【发布时间】:2019-03-16 20:02:05
【问题描述】:

上下文

我正在创建一个 RecyclerAdapter 来显示某一天的预测信息。我的 RecyclerView 包含多天,每一天都使用 onBindViewHolder 进行修改。

每天的布局有 3 个文本视图。第一个包含一个作为摘要的字符串。第二个包含一个带有双精度的字符串作为表示低温的位置参数。第三个与第二个相同,但代表高温。

下面是我的 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());
}

问题

由于高温和低温是doubles,我需要相应地格式化字符串,以免我只用一个double值覆盖字符串。以下是高温和低温的字符串资源:

<string name="temperature_high">High of %1$.2f</string>
<string name="temperature_low">Low of %1$.2f</string>

在 RecyclerAdapter 类之外我知道如何做到这一点,下面是我如何在 Fragment 中格式化字符串的示例:

 String moddedString = String.format(getString(R.string.temperature), temp);
 ((TextView)activity.findViewById(R.id.temperatureDisplay)).setText(moddedString);

但是,我无法访问 RecyclerAdapter 中的 getString() 函数,因此我无法适当地格式化字符串以插入我需要的温度,而无需用双精度完全覆盖字符串。

问题

如何在onBindViewHolder() 方法中使用getString()

【问题讨论】:

  • 旁注。 Context 类中有getString(int, Object...) 方法。所以你可以使用getString(R.string.temperature, temp) 而不是String.format(getString(R.string.temperature), temp)

标签: android android-studio android-recyclerview


【解决方案1】:

你可以使用上下文获取字符串资源。

  context.getString(R.string.temperature)

【讨论】:

  • 我所做的是在我的回收器适配器中将 context 作为实例变量,然后在 onCreateViewHolder() 方法中分配它。这间接地有所帮助。
  • 无需保存对Context的直接引用;您可以按需访问ViewHolder 的上下文。当然,这样做也没有什么真正错误
【解决方案2】:

如何在 onBindViewHolder() 方法中使用 getString()?

每个ViewHolder 实例都有一个itemView 字段,它是View 的一个实例。每个View 实例都有一个getContext() 方法;您可以使用它来访问资源。

String text = viewHolder.itemView.getContext().getString(R.string.mystring);

【讨论】:

  • 这个答案更好,因为它是独立的,并且不需要额外的推断来使其工作。此外,正如我对@Ramesh 的回答所评论的那样,我不必在方法的本地范围之外公开上下文。
  • 在适配器中访问上下文的最佳答案。
【解决方案3】:

您可以使用 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);
    }

【讨论】:

    【解决方案4】:
    //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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-21
      • 1970-01-01
      • 2020-04-14
      • 1970-01-01
      相关资源
      最近更新 更多