这是我的代码,我希望,当我单击firebase recyclerview中的任何帖子时,它应该在新活动中打开,并且变量(mtextview)应传递到新活动活动,请帮助我...
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);
}
}
要解决此问题,请使用以下代码:
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());
}
});
}
}
您可以看到,我在您的myViewHolder
类中添加了一种新方法,名为setChapterTitle()
。此方法将帮助您将章节的标题设置为TextView
。我还使用了setOnClickListener
。因此,每次单击视图时,标题都会被敬酒。