错误:类'PersonViewHolder'必须声明为abstract或在'OnClickListener'中实现抽象方法'onClick(View)'
在模拟器中运行时,会显示toast消息但是监听器块似乎无法正常工作。我已经在堆栈溢出中经历了很多线程但是找不到解决方案
适配器类
void PlaySound_Volume(string fname, float fVolume = 1, bool async = false){
DWORD dwFileSize;
BYTE* pFileBytes;
ifstream f(fname, ios::binary);
f.seekg(0, ios::end);
int lim = f.tellg();
dwFileSize = lim;
pFileBytes = new BYTE[lim];
f.seekg(0, ios::beg);
f.read((char *)pFileBytes, lim);
f.close();
BYTE* pDataOffset = (pFileBytes + 40);
__int8 * p = (__int8 *)(pDataOffset + 8);
for (int i = 80 / sizeof(*p); i < dwFileSize / sizeof(*p); i++){
// COMMENT FOLLOWING LINE
p[i] = (__int8)((float)p[i] * fVolume);
}
if (async)
PlaySound((LPCSTR)pFileBytes, NULL, SND_MEMORY | SND_ASYNC);
else
PlaySound((LPCSTR)pFileBytes, NULL, SND_MEMORY);
}
Recycler.java
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {
private String pName;
private String pAge;
private Context context;
private List<Person> persons;
private OnItemClickListener listener;
public static class PersonViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
CardView cv;
TextView personName;
TextView personAge;
TextView personRegno;
TextView personOPno;
TextView personDocName;
TextView personGender;
ImageView personPhoto;
private OnItemClickListener listener;
PersonViewHolder(final View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cv);
personName = (TextView) itemView.findViewById(R.id.person_name);
personAge = (TextView) itemView.findViewById(R.id.person_age);
personRegno = (TextView) itemView.findViewById(R.id.person_Reg_no);
personOPno = (TextView) itemView.findViewById(R.id.person_op_no);
personDocName = (TextView) itemView.findViewById(R.id.person_doctor_name);
personPhoto = (ImageView) itemView.findViewById(R.id.person_photo);
personGender = (TextView) itemView.findViewById(R.id.person_gender);
final Context context = itemView.getContext();
cv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Toast.makeText(view.getContext(), "ithu thaan mudiyum", Toast.LENGTH_SHORT).show();
if(listener!=null)
listener.onItemClick(personName.getText().toString(),personAge.getText().toString());
}
});
}
}
public RVAdapter(Context context, List<Person> persons, String pName, String pAge) {
this.context = context;
this.pName = pName;
this.pAge = pAge;
this.persons = persons;
}
@Override
public PersonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.cardview_item, null);
return new PersonViewHolder(view);
}
@Override
public void onBindViewHolder(PersonViewHolder holder, int position) {
//getting the product of the specified position
// Person persons = Person.get(position);
//binding the data with the viewholder views
holder.personName.setText(persons.get(position).getName());
holder.personAge.setText(persons.get(position).getAge());
holder.personRegno.setText(persons.get(position).getRegno());
holder.personOPno.setText(persons.get(position).getopno());
holder.personDocName.setText(persons.get(position).getDoctorname());
holder.personGender.setText(persons.get(position).getGender());
holder.personPhoto.setImageDrawable(context.getResources().getDrawable(persons.get(position).getImage()));
holder.setOnItemClickListener(listener);
}
@Override
public int getItemCount() {
return persons.size();
}
public void setOnItemClickListener(OnItemClickListener listener){
this.listener = listener;
}
public interface OnItemClickListener{
public void onItemClick(String personName, String personAge);
}
}
这是我第一次发帖提问。所以请忽略格式化问题。
答案 0 :(得分:0)
如果您实施onClick
,则需要实施View.OnClickListener
方法,但在您的情况下,您需要设置如下的监听器
cv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Toast.makeText(view.getContext(), "ithu thaan mudiyum", Toast.LENGTH_SHORT).show();
if(listener!=null)
listener.onItemClick(personName.getText().toString(),personAge.getText().toString());
}
});
因此,您需要删除implements View.OnClickListener
或覆盖其视图持有者类中的onClick()
方法,如下所示
@Override
public void onClick(View v) {
}