在@override onBindViewHolder
方法,我得到一個錯誤信息:RecyclerView適配器的Android
非靜態字段「mWeatherTextView」不能從 靜態上下文
我檢查了所有可能的錯誤引用但沒有找到任何,即使我的代碼與提供的解決方案類似,但沒有提供解決方案的錯誤。
package com.example.android.sunshine;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ForecastAdapter extends
RecyclerView.Adapter<ForecastAdapter.ForecastAdapterViewHolder> {
private String[] mWeatherData;
public ForecastAdapter(){}
public class ForecastAdapterViewHolder extends RecyclerView.ViewHolder{
public final TextView mWeatherTextView;
public ForecastAdapterViewHolder(View view){
super(view);
mWeatherTextView =(TextView)view.findViewById(R.id.tv_weather_data);
}
}
@Override
public ForecastAdapterViewHolder onCreateViewHolder(ViewGroup parent, int
viewType) {
Context context = parent.getContext();
int layoutIdForListItem = R.layout.forecast_list_item;
boolean shouldAttachToParentImmediately = false;
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(layoutIdForListItem,
parent,shouldAttachToParentImmediately);
return new ForecastAdapterViewHolder(view);
}
@Override
public void onBindViewHolder(ForecastAdapterViewHolder holder, int position)
{
String weatherForThisDay = mWeatherData[position];
ForecastAdapterViewHolder.mWeatherTextView.setText(weatherForThisDay);
_**/* error message says "Non-static field 'mWeatherTextView' cannot be referenced from a static context" */**_
}
@Override
public int getItemCount() {
if(mWeatherData == null) return 0;
return mWeatherData.length;
}
}
Thanku ...您的編輯解決了我的問題 –