2017-10-01 123 views
0

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

回答

0

onBindViewHolder,你總是會使用已作爲參數傳遞的holder。所以在setText行中將ForecastAdapterViewHolder更改爲holder

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]; 
    holder.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; 
} 
} 
+0

Thanku ...您的編輯解決了我的問題 –

0

而不是ForecastAdapterViewHolder.mWeatherTextView.setText(weatherForThisDay);

使用

holder.mWeatherTextView.setText(weatherForThisDay); 
+0

Thanku ...您的編輯解決我的問題.... :(但是計算器允許綠色對勾只爲一個正確的答案。即使相同的答案,提出了多次... –

相關問題