2 回答
TA贡献1796条经验 获得超10个赞
这是因为您正在分配“自身的上下文”。从初始化适配器的类中传递上下文。
假设我有名为“ClubAdapter”的适配器和名为“ClubClass”的调用类
在俱乐部类:
ClubAdapter clubAdapter = new ClubAdapter(list, context);
// if you'r calling it from activity then pass "Activity.this" and if fragment then "getContext()" as context.
并在 ClubAdapter
public class ClubAdapter extends RecyclerView.Adapter<ClubAdapter.ClubHolder> {
private Context context;
private List<String> list;
public ClubAdapter(List<String> list, Context context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public ClubHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.item_sample, viewGroup, false);
return new ClubHolder(itemView, context);
}
public class ClubHolder extends RecyclerView.ViewHolder {
private Context context;
private ClunHolder(@NonNull View itemView,Context context) {
super(itemView);
this.context = context;
}
}
}
或者你可以制作这样的界面。
public interface RecycleViewOnItemClickListener {
void onItemClick(int position);
}
并将其从调用类传递给您的适配器。
假设我有名为“SampleAdapter”的适配器并调用名为“SampleClass”的类在“SampleClass”中,您可以将适配器中的接口作为参数传递。
SampleAdapter sampleAdapter = new SampleAdapter(list, new RecycleViewOnItemClickListener() {
@Override
public void onItemClick(int position) {
// do what you want to perform
}
});
在“俱乐部适配器”中
public class ClubAdapter extends RecyclerView.Adapter<ClubAdapter.ClubHolder> {
private RecycleViewOnItemClickListener recycleViewOnItemClickListener;
private List<String> list;
public ClubAdapter(List<String> list, RecycleViewOnItemClickListener recycleViewOnItemClickListener) {
this.list = list;
this.recycleViewOnItemClickListener = recycleViewOnItemClickListener;
}
@NonNull
@Override
public ClubHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.item_sample, viewGroup, false);
return new ClubHolder(itemView, recycleViewOnItemClickListener);
}
public class ClubHolder extends RecyclerView.ViewHolder {
private RecycleViewOnItemClickListener recycleViewOnItemClickListener;
private ClunHolder(@NonNull View itemView,RecycleViewOnItemClickListener recycleViewOnItemClickListener) {
super(itemView);
this.recycleViewOnItemClickListener = recycleViewOnItemClickListener;
//you can callback onItemClick method from here on button click event so it give callback in "ClubActivity".
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
recycleViewOnItemClickListener.onItemClick(getAdapterPosition());
}
});
}
}
}
TA贡献1817条经验 获得超6个赞
要解决此问题,请在方法中移动以下代码行,onBindViewHolder()如下所示:
holder.btnDetails.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(context, ViewSingleClub.class);
i.putExtra("id", clubId);
context.startActivity(i);
}
});
看,我在holder.btnDetails.
编辑:
根据您的意见,还请进行以下操作。将构造函数更改为:
public ClubAdapter1(@NonNull FirestoreRecyclerOptions<Club> options, Context context) {
super(options);
this.context = context;
}
当您在活动中创建适配器类的对象时,还将 传递context给构造函数。
添加回答
举报