Cick Listener in Firebase RecyclerView

android firebase firebase-realtime-database android-recyclerview firebaseui

49 观看

1回复

1 作者的声誉

here is my code, I want that, when I click on any post in firebase recyclerview, it should open in a new activity and also the variable(mTextView) should pass through to the new activity, kindly help me guys...

recyclerView = (RecyclerView) findViewById(R.id.rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        FirebaseDatabase.getInstance().setPersistenceEnabled(true);

        final FirebaseDatabase database = FirebaseDatabase.getInstance();
        myRef = database.getReference("Data");

        FirebaseRecyclerAdapter<Chapter,myViewHolder> adapter = new FirebaseRecyclerAdapter<Chapter, myViewHolder>(
                Chapter.class, R.layout.item_row, myViewHolder.class, myRef) {

            @Override
            protected void populateViewHolder(myViewHolder viewHolder, Chapter model, int position) {
                viewHolder.mTextView.setText(model.getTitle());
            }
        };
        recyclerView.setAdapter(adapter);
    }
    public static class myViewHolder extends RecyclerView.ViewHolder{
        private TextView mTextView;

        public myViewHolder(View itemView) {
            super(itemView);
            mTextView = (TextView) itemView.findViewById(R.id.titleText);

        }
    }
作者: Nadeem Taj 的来源 发布者: 2017 年 12 月 27 日

回应 1


0

58768 作者的声誉

To solve this, please use the following code:

public static class myViewHolder extends RecyclerView.ViewHolder{
    private TextView mTextView;

    public myViewHolder(View itemView) {
        super(itemView);
        mTextView = (TextView) itemView.findViewById(R.id.titleText);

    }

    void setChapterTitle(Chapter chapter){
        mTextView.setText(chapter.getTitle());

        mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("TAG", "You clicked on: " + chapter.getTitle());
            }
        });         
    }
}

As you can see, I have added a new method in your myViewHolder class named setChapterTitle(). This method will help you set the title of the Chapter to the TextView. I used also a setOnClickListener. So every time you click on a view, the title will be toasted.

作者: Alex Mamo 发布者: 2017 年 12 月 28 日
32x32