所以我有一个 RecycleView,我要用 FirebaseRecyclerViewAdapter 填充。
我正在关注这个例子:https ://github.com/firebase/firebaseui-android#using-a-recyclerview
不过,我遇到了一个奇怪的错误:
java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView 不能转换为 android.view.ViewGroup
这就是我在 onCreateView 中的内容:
RecyclerView recycler = (RecyclerView) mView.findViewById(R.id.rvTasks);
recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
FirebaseRecyclerViewAdapter mAdapter =
new FirebaseRecyclerViewAdapter<Task, TaskViewHolder>(Task.class, android.R.layout.simple_list_item_1, TaskViewHolder.class, mRef) {
@Override
public void populateViewHolder(TaskViewHolder taskViewHolder, Task task) {
taskViewHolder.taskText.setText(task.getText());
}
};
recycler.setAdapter(mAdapter);
这是 ViewHolder:
私有静态类 TaskViewHolder 扩展 RecyclerView.ViewHolder { TextView taskText;
public TaskViewHolder(View itemView) {
super(itemView);
taskText = (TextView)itemView.findViewById(android.R.id.text1);
}
}
这是任务类:
public class Task {
String author;
String text;
public Task() {
}
public Task(String author, String text) {
this.author = author;
this.text = text;
}
public String getAuthor() {
return author;
}
public String getText() {
return text;
}
}
有任何想法吗?提前致谢!