So I have a RecycleView that I'm to fill with a FirebaseRecyclerViewAdapter.
I'm following this example: https://github.com/firebase/firebaseui-android#using-a-recyclerview
I'm getting a strange error, though:
java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.view.ViewGroup
This is what I have in 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);
This is the ViewHolder:
private static class TaskViewHolder extends RecyclerView.ViewHolder { TextView taskText;
public TaskViewHolder(View itemView) {
super(itemView);
taskText = (TextView)itemView.findViewById(android.R.id.text1);
}
}
This is the Task class:
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;
}
}
Any ideas? Thanks in advance!
You should have really placed more of the error on your post. It would have shown where the issue was happening. However, I will try to make an educated guess after looking at he source for
FirebaseRecyclerViewAdapter
.The issue appears to be with the layout that you passes to the adapter,
android.R.layout.simple_list_item_1
. The adapter expects a layout that is wrapped by a subclass ofViewGroup
, such as aLinearLayout
,FrameLayout
or some other type of Layout.android.R.layout.simple_list_item_1
is implemented like this:As you can see, the
TextView
is not wrapped inside of a Layout. The quickest way to fix the error would be to create your own layout, with aTextView
inside of aFrameLayout
like so:You would then pass the layout you created to the adapter. Lets call it
my_simple_list_item_1.xml
, which you would pass to the adapter asR.layout.my_simple_list_item_1
.