在我的片段类
中 public class ReceiveFragment extends Fragment implements ReceiverRecyclerViewAdapter.OnItemClick {
btnGenAddress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
linearLayoutMyWalletInfo.setVisibility(View.VISIBLE);
linearLayoutGenAddress.setVisibility(View.GONE);
recyclerViewAdapter = new ReceiverRecyclerViewAdapter(getActivity(),subjects, emails,this);
recyclerViewCardContainer.setAdapter(recyclerViewAdapter);
}
});
@Override
public void onClick(String value) {
}
并且在ReceiverRecyclerViewAdapter类中我创建了一个接口,现在当我在片段中写入 recyclerViewAdapter = new ReceiverRecyclerViewAdapter(getActivity(),subject,emails,this); } 当我使用“this”参数时,它会给出complie时间错误。
和我的ReceiverRecyclerViewAdapter类
public class ReceiverRecyclerViewAdapter extends RecyclerView.Adapter<ReceiverRecyclerViewAdapter.ViewHolder>{
private OnItemClick mCallback;
public ReceiverRecyclerViewAdapter(Context context1,String[] SubjectValues1, String[] SubjectValues2,OnItemClick listener){
SubjectValues = SubjectValues1;
EmailValues2 = SubjectValues2;
context = context1;
this.mCallback = listener;
}
public interface OnItemClick {
void onClick(String value);
}
itemView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// get position
int pos = getAdapterPosition();
// check if item still exists
if(pos != RecyclerView.NO_POSITION){
String clickedDataItem = SubjectValues[pos];
Toast.makeText(v.getContext(), "You clicked " + clickedDataItem, Toast.LENGTH_SHORT).show();
mCallback.onClick(clickedDataItem);
}
}
});
}
答案 0 :(得分:3)
您从片段中传递了4个参数,但构造函数需要3个。因此,您需要在适配器中添加Context
作为第一个参数。
更新:
您正在this
实例中传递一个匿名类OnClickListener
,该类未实现该接口。所以不是这样,传递ReceiveFragment.this
,这样你就引用了实现接口的片段:
recyclerViewAdapter = new ReceiverRecyclerViewAdapter(getActivity(),subjects, emails, ReceiveFragment.this);
答案 1 :(得分:2)
问题是因为当您从OnClickListener传递this
时,它正在传递on click实例而不是该接口。
您可以在片段中初始化界面,如下面的代码,并将其传递给适配器。
OnItemClick onitemClick = (OnItemClick) this;
btnGenAddress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
linearLayoutMyWalletInfo.setVisibility(View.VISIBLE);
linearLayoutGenAddress.setVisibility(View.GONE);
recyclerViewAdapter = new ReceiverRecyclerViewAdapter(getActivity(),subjects, emails,onitemClick);
recyclerViewCardContainer.setAdapter(recyclerViewAdapter);
}
});
public class ReceiverRecyclerViewAdapter extends RecyclerView.Adapter<ReceiverRecyclerViewAdapter.ViewHolder>{
private OnItemClick mCallback;
public ReceiverRecyclerViewAdapter(Context context, String[] SubjectValues1, String[] SubjectValues2,OnItemClick listener){
SubjectValues = SubjectValues1;
EmailValues2 = SubjectValues2;
this.context = context;
this.mCallback = listener;
}
public interface OnItemClick {
void onClick(String value);
}
itemView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// get position
int pos = getAdapterPosition();
// check if item still exists
if(pos != RecyclerView.NO_POSITION){
String clickedDataItem = SubjectValues[pos];
Toast.makeText(v.getContext(), "You clicked " + clickedDataItem, Toast.LENGTH_SHORT).show();
mCallback.onClick(clickedDataItem);
}
}
});
}
答案 2 :(得分:1)
试试这个:
在适配器中:
撰写public ReceiverRecyclerViewAdapter(Context context1,String[] SubjectValues1, String[] SubjectValues2,OnItemClick listener)
片段:
制作一个对象:OnItemClick onItemClick;
在构造函数中:this.OnItemClick = onItemClick;
将此对象传递给适配器。
活动:
实施此listner
。