java—如何在一个有效的recyclerview中实现onclicklistener?

xbp102n0  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(318)

我已经搜索了internet平台,甚至在stackoverflow上,在recyclerview中找到了一个onclicklistener的工作实现,但没有任何运气。
所以除非我不能把森林换成树,否则我就被困住了!
我将“implements view.onclicklistener”添加到(public)类viewholder(在适配器中):

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener

在那个类中,我向一些元素声明了变量:

TextView tvTimeBlockDate, tvTimeBlockBlock;
Button btnTimeBlockRemove;

当然,还有一个public viewholder,在其中,我将这些变量连接到资源:

tvTimeBlockDate = itemView.findViewById(R.id.tvTimeBlockDate);
    tvTimeBlockBlock = itemView.findViewById(R.id.tvTimeBlockBlock);
    btnTimeBlockRemove = itemView.findViewById(R.id.btnTimeBlockRemove);

然后我创建了一个onclicklistener:

btnTimeBlockRemove.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });

如果我加上

tvTimeBlockDate.setText("Hello");

对于onclicklistener来说,一切都很好:我可以通过单击按钮来更改textview,没有什么特别的。

In my Activity, I created a void:
    public void btnRemoveClick(final String strValue)
    {
        Toast.makeText(getApplicationContext(),strValue,Toast.LENGTH_LONG);

    }

我想从我的适配器访问该空白。但我该怎么做呢?我在onclicklistener中尝试了以下操作:

((TimeManager) context).btnRemoveClick("Test");

(注:显然,该活动称为时间管理器)
但是应用程序在特定的线路上崩溃,并重新启动应用程序。那我做错什么了?
阿尔伯特范哈顿丹麦
根据@zain的请求,我将我的整个适配器添加到问题中。请注意,我遇到过这个问题,所以这个java类中可能有太多的内容,我会在一切正常时进行整理:

package com.kairosplanner.app

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class TimeBlockAdapter extends RecyclerView.Adapter<TimeBlockAdapter.ViewHolder>
{

    private Context context;
    private ArrayList<TimeBlock> timeblock;

    public TimeBlockAdapter(Context context, ArrayList<TimeBlock> list)
    {
        this.context = context;
        timeblock = list;

    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
    {
        TextView tvTimeBlockDate, tvTimeBlockBlock;
        Button btnTimeBlockRemove;
        private Context context;

        public ViewHolder(@NonNull final View itemView) {
            super(itemView);

            tvTimeBlockDate = itemView.findViewById(R.id.tvTimeBlockDate);
            tvTimeBlockBlock = itemView.findViewById(R.id.tvTimeBlockBlock);
            btnTimeBlockRemove = itemView.findViewById(R.id.btnTimeBlockRemove);
            //Button btnRemove = itemView.findViewById(R.id.btnTimeBlockRemove);

            btnTimeBlockRemove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    ((TimeManager) context).btnRemoveClick("Test");
                    //The above line causes a crash - try to debug

                    //tvTimeBlockDate.setText("Hello");
                    //itemView.btnRemoveClick();

                }
            });
        }
            @Override
            public void onClick(View view) {

        }
    }

    @NonNull
    @Override
    public TimeBlockAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.timeblock_items, viewGroup, false);

        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull TimeBlockAdapter.ViewHolder viewHolder, int i) {

        viewHolder.itemView.setTag(timeblock.get(i));

        viewHolder.tvTimeBlockDate.setText(timeblock.get(i).getStrDate());
        viewHolder.tvTimeBlockBlock.setText(timeblock.get(i).getStrTimeBlock());
    }

        @Override
    public int getItemCount() {
        return timeblock.size();
    }
}
ws51t4hk

ws51t4hk1#

适配器如下所示:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class TimeBlockAdapter extends RecyclerView.Adapter<TimeBlockAdapter.ViewHolder>
{

    private Context mContext;
    private ArrayList<TimeBlock> timeblock;
    private String EncKey;

    public TimeBlockAdapter(Context context, ArrayList<TimeBlock> list, String strEncKey)
    {
        this.mContext = context;
        timeblock = list;
        EncKey = strEncKey;
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
    {
        TextView tvTimeBlockDate, tvTimeBlockBlock;
        Button btnTimeBlockRemove;

        public ViewHolder(@NonNull final View itemView) {
            super(itemView);

            tvTimeBlockDate = itemView.findViewById(R.id.tvTimeBlockDate);
            tvTimeBlockBlock = itemView.findViewById(R.id.tvTimeBlockBlock);
            btnTimeBlockRemove = itemView.findViewById(R.id.btnTimeBlockRemove);

            btnTimeBlockRemove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   if (mContext instanceof TimeManager) {
                        ((TimeManager) mContext).btnRemoveClick(EncKey);
                    }
                }
            });
        }
            @Override
            public void onClick(View view) {

        }
    }

    @NonNull
    @Override
    public TimeBlockAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.timeblock_items, viewGroup, false);

        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull TimeBlockAdapter.ViewHolder viewHolder, int i) {

        viewHolder.itemView.setTag(timeblock.get(i));

        viewHolder.tvTimeBlockDate.setText(timeblock.get(i).getStrDate());
        viewHolder.tvTimeBlockBlock.setText(timeblock.get(i).getStrTimeBlock());
    }

        @Override
    public int getItemCount() {
        return timeblock.size();
    }
}

在活动中,我添加了以下内容

public void btnRemoveClick(final String strEncKey)
{
    Toast.makeText(getApplicationContext(),"Hello, the key you are using is "+strEncKey,Toast.LENGTH_LONG).show();

}

这就行了!

相关问题