Android RecyclerView:为什么Adapter的上下文返回警告?

时间:2016-07-31 00:47:59

标签: android android-recyclerview android-adapter

我有一个Adapter RecyclerView文件,可以提供此Android Studio警告: Private field 'context' is assigned but never accessed即使我使用this.context = context。我有另一个有Adapter文件的工作应用程序,该文件与context具有完全相同的代码,Android Studio不会对该文件发出警告。我在这里缺少什么?

带有警告的

ContactListAdapter.java文件:

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<>();
}    

这是工作应用的ContactListAdapter.java文件,没有工作室警告:

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<>();
}

1 个答案:

答案 0 :(得分:0)

此警告表示您不在课堂上使用context字段。如果你不需要它,你可以删除它。除非你不删除它,否则不会有问题。