无法在Adapter类中实现View.onClickListener

android adapter onclicklistener android-cardview recycler-adapter

373 观看

1回复

17 作者的声誉

错误:类'PersonViewHolder'必须声明为abstract或在'OnClickListener'中实现抽象方法'onClick(View)'

在模拟器中运行时,会显示Toast消息但是侦听器块似乎无法正常工作。我已经在堆栈溢出中经历了很多线程但是找不到解决方案

适配器类

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);
}
}

Recycler.java

public class Recycler extends Fragment {

public Recycler() {

}


List<Person> persons;
private String pName;
private String pAge;

RecyclerView rv;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View rootview = inflater.inflate(R.layout.recycler, container, false);
    super.onCreate(savedInstanceState);

    rv = (RecyclerView)rootview.findViewById(R.id.rv);
    rv.setHasFixedSize(true);
    rv.setLayoutManager(new LinearLayoutManager(getContext()));

    persons = new ArrayList<>();

    persons.add(
            new Person("Vijay", "22", "Male", "RegNo:666", "Doctor:vishnu", "Opno:64446", R.drawable.ic_account_circle_black_18dp));

    persons.add(
            new Person("Vishnu", "23","Male", "RegNo:666", "Doctor:vishnu", "Opno:6666", R.drawable.ic_account_circle_black_18dp));

    persons.add(
            new Person("Vaishnavi sulochana", "24","Female", "RegNo:666", "Doctor:vishnu", "Opno:6666", R.drawable.ic_account_circle_black_18dp));

    persons.add(
            new Person("Ram", "25","Male", "RegNo:666", "Doctor:vishnu", "Opno:6666",R.drawable.ic_account_circle_black_18dp));

    persons.add(
            new Person("Raja", "21","Male","RegNo:666", "Doctor:vishnu", "Opno:6666", R.drawable.ic_account_circle_black_18dp));

    persons.add(
            new Person("Hari", "23", "Male","RegNo:666", "Doctor:vishnu", "Opno:6666",R.drawable.ic_account_circle_black_18dp));

    persons.add(
            new Person("Hari", "23", "Male","RegNo:666", "Doctor:vishnu", "Opno:6666",R.drawable.ic_account_circle_black_18dp));

    //creating recyclerview adapter
    //RVAdapter adapter = new RVAdapter(getContext(), persons);
    RVAdapter adapter = new RVAdapter(getContext(),persons,pName,pAge);
    adapter.setOnItemClickListener(new RVAdapter.OnItemClickListener(){
        public void onItemClick(String textName, String textViewBrief){
            ServiceTable eventFragment = new ServiceTable();
            //replace content frame with your own view.
            FragmentTransaction ft = getFragmentManager().beginTransaction();    ft.replace(R.id.main_container, eventFragment).commit();
        }
    });

    //RVAdapter myAdapter = new RVAdapter(persons, this);

    //setting adapter to recyclerview
    rv.setAdapter(adapter);
    return rootview;
}

}

这是我第一次发帖提问。所以请忽略格式化问题。

作者: Vishnu Roshan 的来源 发布者: 2018 年 1 月 9 日

回应 1


0

774 作者的声誉

决定

如果实现,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) {

}
作者: Ergin Ersoy 发布者: 2018 年 1 月 9 日
32x32