回收器视图适配器:如何修复"Private field mContext is assigned but never accessed"?



我有一个似乎工作正常的回收器视图列表。
但是Android Studio在适配器文件中对"mContext"发出警告,说"私有字段'mContext'被分配但从未访问过"。
但是我给它分配了"this.mContext = context;"。
我在这里错过了什么?

MyRecylerAdapter.java file

public class MyRecylerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final Context mContext;   
    public ArrayList<ListItem> listItems;
    ...
    public MyRecylerAdapter(Context context, ArrayList<ListItem> listItems) {
        this.listItems = listItems;
        this.mContext = context;  // Android Studio doesn't like this assignment.
    }

已分配但从未访问

您从不访问,只分配

例如,这是一个访问权限。您在ListAdapter中要做的事情是膨胀布局。

View itemView = LayoutInflater.from(mContext).inflate(R.layout.foo);

真正的问题:如果您从未访问过Context,为什么还需要它?

相关内容

最新更新