我在第76和42行的Recycler视图适配器上一次又一次重复应用和它们的一个错误
在42行(此代码)
itemClickListener.onClick(v,getAdapterPosition());
在76行(此代码)上
context.startActivity(intent);
。
整个代码:
package com.healthy.kishorsinh.yogaapp.Adapter;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
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 android.widget.Toast;
import com.healthy.kishorsinh.yogaapp.Interface.ItemClickListener;
import com.healthy.kishorsinh.yogaapp.Model.Exercises;
import com.healthy.kishorsinh.yogaapp.R;
import com.healthy.kishorsinh.yogaapp.ViewExercises;
import java.util.List;
class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ImageView image;
public TextView text;
private ItemClickListener itemClickListener;
public RecyclerViewHolder(View itemView) {
super(itemView);
image = (ImageView)itemView.findViewById(R.id.ex_img);
text = (TextView) itemView.findViewById(R.id.ex_name);
itemView.setOnClickListener(this);
}
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
@Override
public void onClick(View v) {
itemClickListener.onClick(v,getAdapterPosition());
}
}
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolder> {
private List<Exercises> exercisesList;
private Context context;
public RecyclerViewAdapter(List<Exercises> exercisesList, Context context) {
this.exercisesList = exercisesList;
this.context = context;
}
@NonNull
@Override
public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.item_exercises,parent,false);
return new RecyclerViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
holder.image.setImageResource(exercisesList.get(position).getImage_id());
holder.text.setText(exercisesList.get(position).getName());
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position) {
Intent intent =new Intent(context, ViewExercises.class);
intent.putExtra("image_id",exercisesList.get(position).getImage_id());
intent.putExtra("name",exercisesList.get(position).getName());
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return exercisesList.size();
}
}
错误:
at com.healthy.kishorsinh.yogaapp.Adapter.RecyclerViewAdapter$1.onClick
(RecyclerViewAdapter.java:76)
at com.healthy.kishorsinh.yogaapp.Adapter.RecyclerViewHolder.onClick
(RecyclerViewAdapter.java:42)
答案 0 :(得分:0)
您是否尝试过更改代码,例如:
class RecyclerViewHolder extends RecyclerView.ViewHolder {
public ImageView image;
public TextView text;
public RecyclerViewHolder(View itemView) {
super(itemView);
image = (ImageView)itemView.findViewById(R.id.ex_img);
text = (TextView) itemView.findViewById(R.id.ex_name);
}
}
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolder> {
private List<Exercises> exercisesList;
private Context context;
public RecyclerViewAdapter(List<Exercises> exercisesList, Context context) {
this.exercisesList = exercisesList;
this.context = context;
}
@NonNull
@Override
public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.item_exercises,parent,false);
return new RecyclerViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
holder.image.setImageResource(exercisesList.get(position).getImage_id());
holder.text.setText(exercisesList.get(position).getName());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent =new Intent(context, ViewExercises.class);
intent.putExtra("image_id",exercisesList.get(position).getImage_id());
intent.putExtra("name",exercisesList.get(position).getName());
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return exercisesList.size();
}
}
我看到您可以同时在两个地方调用侦听器,即使可以通过视点库在一个地方完成