【问题标题】:Why does a call to interface method not work with Capitalized-name-reference to interface instance? [closed]为什么对接口方法的调用不适用于对接口实例的大写名称引用? [关闭]
【发布时间】:2021-08-06 11:56:09
【问题描述】:

请帮帮我,因为这个错误,我无法运行程序。

不能从静态上下文引用非静态方法onClicked(int)

代码

public class ProfileAdapter extends RecyclerView.Adapter<ProfileAdapter.myViewHolder> {

    ArrayList<Profile_Info> infos;
    Context context;
    MyClickedInterface myClickedInterface;


    public ProfileAdapter(ArrayList<Profile_Info> infos, Context context, MyClickedInterface myClickedInterface) {
        this.infos = infos;
        this.context = context;
        this.myClickedInterface = myClickedInterface;
    }

    @NonNull
    @Override
    public ProfileAdapter.myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.my_profile,parent,false);
        return new myViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ProfileAdapter.myViewHolder holder, int position) {
        holder.textView.setText(infos.get(position).getAppName());
        holder.imageView.setImageResource(infos.get(position).getImageResource());
    }

    @Override
    public int getItemCount() {
        return infos.size();
    }
    public class myViewHolder extends RecyclerView.ViewHolder{
        TextView textView;
        ImageView imageView;
        public myViewHolder(@NonNull View itemView) {
            super(itemView);
            textView =(TextView) itemView.findViewById(R.id.text);
            imageView = (ImageView) itemView.findViewById(R.id.image);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    MyClickedInterface.onClicked(getAdapterPosition());
                }
            });
        }

    }

    interface MyClickedInterface{
        void onClicked (int position);
    }
}

【问题讨论】:

  • 这里 --> MyClickedInterface.onClicked(getAdapterPosition());
  • 你需要阅读错误输出,如果你不理解它,请研究错误细节(或至少在这里发布错误输出)。

标签: java android android-recyclerview


【解决方案1】:

您试图通过您的接口MyClickedInterface 调用onClicked,而不是使用实例myClickedInterface 调用它

替换

MyClickedInterface.onClicked(getAdapterPosition());

myClickedInterface.onClicked(getAdapterPosition());

【讨论】:

  • 是的,它成功了!非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-07
  • 2019-05-16
  • 1970-01-01
  • 2020-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多