TAGS :Viewed: 8 - Published at: a few seconds ago

[ Android RecyclerView: why is Adapter's context returning warning? ]

I have an Adapter file for a RecyclerView that gives this Android Studio warning: Private field 'context' is assigned but never accessed even though I use this.context = context. I have another working app with an Adapter file that has the exact same code for the context and Android Studio does not give a warning for that file. What am I missing here?

ContactListAdapter.java file with warning:

package com.example.jdw.sixteenthversion;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class ContactListAdapter extends RecyclerView.Adapter<ContactListAdapter.ListViewHolder> {

private List<Contact> contactList;
**private Context context**;

private RecyclerItemClickListener recyclerItemClickListener;

public ContactListAdapter(Context context) {
    this.context = context;
    this.contactList = new ArrayList<>();
}    

This is the working app's ContactListAdapter.java file and no Studio warning:

package com.wimso.android_sqlite.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wimso.android_sqlite.R;
import com.wimso.android_sqlite.listener.RecyclerItemClickListener;
import com.wimso.android_sqlite.model.Contact;
import com.wimso.android_sqlite.widget.LetterTile;

import java.util.ArrayList;
import java.util.List;

public class ContactListAdapter extends RecyclerView.Adapter<ContactListAdapter.ContactHolder>{

private List<Contact> contactList;
**private Context context**;

private RecyclerItemClickListener recyclerItemClickListener;

public ContactListAdapter(Context context) {
    this.context = context;
    this.contactList = new ArrayList<>();
}

Answer 1


This warning means, you dont use your context field in rest of class. If you dont need it you can delete it. Unless you dont delete it gonna be not a problem.