setItemOnclickListener在FirebaseRecyclerAdapter中不起作用

时间:2018-05-23 21:33:52

标签: android firebase

我是FirebASE的新用户并且坚持 setItemclickListener ,这里有一些片段,请帮助我。主要是在FirebaseRecycler中,其中setItemClickListener是接口并用于Firebaserecy的Onclick

Leavereqests.java

  @Override
//LeaveReqests.java 
    leavereq= database.getReference("LeafForm");
    recyclerview = (RecyclerView)findViewById(R.id.recycler_hod);
    recyclerview.setHasFixedSize(true);
    LinearLayoutManager manager = new LinearLayoutManager(this);
    recyclerview.setLayoutManager(manager);
    loadhod();
}
private void loadhod() {
  adapter = new FirebaseRecyclerAdapter<LeaveReq, HodViewHolder>(LeaveReq.class,R.layout.hod_login_list
          ,HodViewHolder.class,leavereq) {
        @Override
        protected void populateViewHolder(HodViewHolder viewHolder, LeaveReq model, int position) {
             viewHolder.name.setText(model.getName());

            clickitem = model;
            viewHolder.setItemClickListener(new ItemClickListener() { //the non working part 
                @Override
                public void onClick(View view, int position) {
                    Intent hodlist = new Intent(LeaveRequests.this,HodReqList.class);
                    hodlist.putExtra("hodid",adapter.getRef(position).getKey());
                    startActivity(hodlist);
                }
            });
        }
    };
    recyclerview.setAdapter(adapter);}}

HodViewholder.java

public class HodViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public Button name;
private ItemClickListener itemClickListener;

public HodViewHolder(View itemView) {
    super(itemView);
    name=(Button)itemView.findViewById(R.id.name_of_faculty);
    itemView.setOnClickListener(this);
}

public void setItemClickListener(ItemClickListener itemClickListener) {
    this.itemClickListener = itemClickListener;
}

@Override
public void onClick(View view) {
    itemClickListener.onClick(view,getAdapterPosition(),false);

}}

LeaveReq.java是具有名称getter的类。

public class LeaveReq {
private String name;

public LeaveReq() {
}

public LeaveReq(String name) {

    this.name = name;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
}

项目单击“侦听器界面”

public interface ItemClickListener {
void onClick(View view, int position);
}

正在填充列表,但点击操作无法正常工作

1 个答案:

答案 0 :(得分:0)

 @Override
    protected void onStart() {

        super.onStart();
        FirebaseRecyclerAdapter<adapter, PandaViewHolder> MyAdapter = new FirebaseRecyclerAdapter <adapter, PandaViewHolder>(
                adapter.class,
                R.layout.replay_panda,
                HodViewHolder.class,
                mDatabase


        ) {
            @Override
            protected void populateViewHolder(final PandaViewHolder viewHolder, adapter model, int position) {

                final String position_key = getRef( position ).getKey().toString();

                viewHolder.setTitle(model.getTitle());


              //// Here
               //#################################

               viewHolder.mView.setOnClik{....}

              //#################################


            }
        };
        RecyclerView.setAdapter( MyAdapter );
    }


    public static class HodViewHolder  extends RecyclerView.ViewHolder {


        View mView;
        DatabaseReference mDatabase1231223;
        FirebaseAuth mAuth;


        public PandaViewHolder(View itemView) {

            super(itemView);
            mView = itemView;

        }

        // post text view
        public void setTitle(String title) {



        }