RecyclerView OnClick侦听器返回错误

时间:2020-09-23 06:39:21

标签: android android-recyclerview

我正在努力寻找一种从recyclerView适配器以putExtra意向发送所选配方名称的方法。通过我的尝试,我得到了这个错误。

错误:找不到符号 intent.putExtra(“ NAME”,categoryList.getAdapterPosition()。getRecipe_name());
^符号:方法getAdapterPosition()位置:变量category类型列表 列表

这是我到目前为止尝试过的片段

    @Override
    public void onClick(View view) {
        if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
        Intent intent = new Intent(view.getContext(), Recipe.class);
        intent.putExtra("NAME", categoryList.getAdapterPosition().getRecipe_name());
        view.getContext().startActivity(intent);
    }

完整的适配器代码

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

    private List<Category> categoryList;
    private LayoutInflater mInflater;
    private com.stu54259.plan2cook.RecipeCategoryAdapter.ItemClickListener mClickListener;

    // data is passed into the constructor
    RecipeCategoryAdapter(Context context, List<Category> data) {
        this.mInflater = LayoutInflater.from(context);
        this.categoryList = data;
    }

    // inflates the row layout from xml when needed
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.cardview_recipe, parent, false);
        return new ViewHolder(view);
    }

/**
 * Called by RecyclerView to display the data at the specified position. This method should
 * update the contents of the {@link ViewHolder#itemView} to reflect the item at the given
 * position.
 * <p>
 * Note that unlike {@link ListView}, RecyclerView will not call this method
 * again if the position of the item changes in the data set unless the item itself is
 * invalidated or the new position cannot be determined. For this reason, you should only
 * use the <code>position</code> parameter while acquiring the related data item inside
 * this method and should not keep a copy of it. If you need the position of an item later
 * on (e.g. in a click listener), use {@link ViewHolder#getAdapterPosition()} which will
 * have the updated adapter position.
 * <p>
 * Override {@link #onBindViewHolder(ViewHolder, int, List)} instead if Adapter can
 * handle efficient partial bind.
 *
 * @param holder   The ViewHolder which should be updated to represent the contents of the
 *                 item at the given position in the data set.
 * @param position The position of the item within the adapter's data set.
 */
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    holder.myTextView1.setText(categoryList.get(position).getRecipe_name());
    holder.myTextView2.setText(categoryList.get(position).getCategory_name());
    holder.imgImage.setImageResource(categoryList.get(position).getImage());

}


    // total number of rows
    @Override
    public int getItemCount() {
        return categoryList.size();
    }


    // stores and recycles views as they are scrolled off screen
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView myTextView1;
        TextView myTextView2;
        ImageView imgImage;

        ViewHolder(View itemView) {
            super(itemView);
            myTextView1 = itemView.findViewById(R.id.txtName);
            myTextView2 = itemView.findViewById(R.id.txtCategory);
            imgImage = itemView.findViewById(R.id.imgImage);
            itemView.setOnClickListener(this);


        }

        @Override
        public void onClick(View view) {
            if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
            Intent intent = new Intent(view.getContext(), Recipe.class);
            intent.putExtra("NAME", categoryList.getAdapterPosition().getRecipe_name());
            view.getContext().startActivity(intent);
        }
    }


    // allows clicks events to be caught
    void setClickListener(RecipeCategoryAdapter.ItemClickListener itemClickListener) {
        this.mClickListener = itemClickListener;
    }

    // parent activity will implement this method to respond to click events
    public interface ItemClickListener {
        void onItemClick(View view, int position);

    }
}

2 个答案:

答案 0 :(得分:2)

如果您需要使用意图将数据发送到另一个活动中,则只需在视图持有者类中执行此操作。

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView myTextView1;
    TextView myTextView2;
    ImageView imgImage;

    ViewHolder(View itemView) {
        super(itemView);
        myTextView1 = itemView.findViewById(R.id.txtName);
        myTextView2 = itemView.findViewById(R.id.txtCategory);
        imgImage = itemView.findViewById(R.id.imgImage);
        itemView.setOnClickListener(this);

itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(view.getContext(),Your_activity);
                intent.putExtra("recipe_name", myTextView1.gettext().toString); //you used recipe name with this id (myTextView1) so.
                view.getContext().startActivity(intent);
            }
        });
    }
}

您也可以在onbindview中实现上述Click侦听器代码。但是,当您单击任何特定项目时,它会检查所有视图持有者,因此,为了克服这一点,我们可以在上述视图持有者类中执行此操作。请让我知道它是否有效。并接受答案(如果它对您有用)。

答案 1 :(得分:0)

然后以这种方式使用

   @Override
        public void onBindViewHolder (@NonNull ViewHolder holder,int position){
            holder.myTextView1.setText(categoryList.get(position).getRecipe_name());
            holder.myTextView2.setText(categoryList.get(position).getCategory_name());
            holder.imgImage.setImageResource(categoryList.get(position).getImage());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    startActivity(new Intent(ShiftHistory.this, Recipe.class)
                            .putExtra("NAME", categoryList.get(position).getRecipe_name()));
                }
            });

        }
    }