2016-04-10 14 views
0

私のRecyclerView.ViewHolderでは、ネットワークが存在するかどうかをチェックし、trueを返す場合はプログレスバーを再表示します。RecyclerView.ViewHolderにコンテキストパラメータを供給する方法

私は、ネットワークの接続性を確認するために、このクラスを使用しますとラインif(NetworkCheck.isAvailableAndConnected(Context)) {のAndroidメーカー赤い下線コンテキスト

public static class ProgressViewHolder extends RecyclerView.ViewHolder { 
     Button loadButton; 
     ProgressBar progressBar; 
     public ProgressViewHolder(View footerView){ 
      super(footerView); 
      loadButton = (Button) footerView.findViewById(R.id.reload_button); 
      progressBar = (ProgressBar) footerView.findViewById(R.id.progress_load); 

      if(NetworkCheck.isAvailableAndConnected(Context)) { 
       loadButton.setVisibility(View.VISIBLE); 

      } 
     } 
    } 

public class NetworkCheck { 

    public static boolean isAvailableAndConnected(Context context) { 
     ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
     boolean isNetworkAvailable = cm.getActiveNetworkInfo() != null; 
     boolean isNetWorkConnected = isNetworkAvailable && cm.getActiveNetworkInfo().isConnected(); 

     return isNetWorkConnected; 

    } 

} 

が続いRecyclerView.ViewHolderに私はこれをやっていますメッセージは「表現が期待されます。私は試しましたcontextgetApplicationContextgetBaseContextthisしかし、それらのどれも動作しているようです。

どうしたら間違っていますか?

+0

あなたのビューホルダーだから、コンテキストを渡す、あなたが拡張しているアダプタに入れて、静的ですあなたはこのホルダーを膨らませていた –

答えて

0

あなたはfooterViewオブジェクトを使用してコンテキストをretriveことができます。

public static class ProgressViewHolder extends RecyclerView.ViewHolder { 
       //... 
       public ProgressViewHolder(View footerView){ 
        if(NetworkCheck.isAvailableAndConnected(footerView.getContext())) { 
         // Do stuff 

       } 
      } 
2

getContextは、アクティビティ、フラグメント、またはビューでのみ使用できます。

あなたの場合は、itemViewをビューホルダーに渡してください。

またはitemView.getContextのいずれかを使用して、Viewインスタンスのメソッドを呼び出します。

+0

私に例を教えてもらえますか? – X09

+1

関数を呼び出してコンテキストを取得するために、最後の段落の2つの例のいずれかに括弧を追加するだけです... –

+0

ありがとう、それは働いた – X09

0

あなたがinflateあなたitemViewからContextオブジェクトを必要とし、あなたがContextオブジェクトあなたがそれを必要とするたびに、このような何かへの参照を持っている。この方法をonCreateViewHolder方法でホルダーを作成し、だからあなたのアダプタでインスタンスを維持知っているように:

public class Adapter extends RecyclerView.Adapter<Adapter.ProgressViewHolder> { 
     Context mContext //you need this to inflate views 
     public Adapter(Context context, arguments you need...) { 
       mContext = context; //here you keep an reference to Context object 
       ... 
     } 
    ... 
    static class ProgressViewHolder extends RecyclerView.ViewHolder { 
    //your implementation 
    } 
} 
関連する問題