I have been experiencing difficulties getting my Android app's RecyclerView to populate the FirebaseUI adapter with data already in the Firebase database, although I can send data to it using:
public void onAdd(View v){
//create Firebase Object
t=(EditText) findViewById(R.id.display);
String mitems=t.getText().toString();
myRef.push().setValue(mitems.toString());
}
I used the following with the aim of receiving my sent data but it is not working:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Recycler
recyclerView=(RecyclerView)findViewById(R.id.messageRecyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//Adapter
FirebaseRecyclerAdapter<String, MessageViewHolder> mFirebaseAdapter=
new FirebaseRecyclerAdapter<String, MessageViewHolder>(
String.class,
android.R.layout.simple_list_item_1,
MessageViewHolder.class,
myRef.child("Message")
) {
@Override
protected void populateViewHolder(MessageViewHolder viewHolder, String model, int position) {
viewHolder.messageTextView.setText(model.toString());
}
};
recyclerView.setAdapter(mFirebaseAdapter);
}
My ViewHolder is defined thus:
static class MessageViewHolder extends RecyclerView.ViewHolder {
public TextView messageTextView;
MessageViewHolder(View view) {
super(view);
messageTextView = (TextView)view.findViewById(android.R.id.text1);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/messageRecyclerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:hint="Enter Text"
android:id="@+id/display"
android:textColor="@color/colorAccent"
android:background="@color/colorPrimary"
android:padding="10dp"
android:layout_width="match_parent" android:layout_height="wrap_content"/>
<Button android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="ADD ITEM"
android:onClick="onAdd"
/>
</LinearLayout>
Here is The Error Message:
Exception java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.view.ViewGroup
com.firebase.ui.database.FirebaseRecyclerAdapter.onCreateViewHolder (FirebaseRecyclerAdapter.java:160)
android.support.v7.widget.RecyclerView$Adapter.createViewHolder (RecyclerView.java:6073)
android.support.v7.widget.RecyclerView$Recycler.getViewForPosition (RecyclerView.java:5243)
android.support.v7.widget.RecyclerView$Recycler.getViewForPosition (RecyclerView.java:5153)
android.support.v7.widget.LinearLayoutManager$LayoutState.next (LinearLayoutManager.java:2061)
android.support.v7.widget.LinearLayoutManager.layoutChunk (LinearLayoutManager.java:1445)
android.support.v7.widget.LinearLayoutManager.fill (LinearLayoutManager.java:1408)
android.support.v7.widget.LinearLayoutManager.onLayoutChildren (LinearLayoutManager.java:580)
android.support.v7.widget.RecyclerView.dispatchLayoutStep2 (RecyclerView.java:3374)
android.support.v7.widget.RecyclerView.dispatchLayout (RecyclerView.java:3183)
android.support.v7.widget.RecyclerView.consumePendingUpdateOperations (RecyclerView.java:1593)
android.support.v7.widget.RecyclerView$1.run (RecyclerView.java:323)
android.view.Choreographer$CallbackRecord.run (Choreographer.java:788)
android.view.Choreographer.doCallbacks (Choreographer.java:591)
android.view.Choreographer.doFrame (Choreographer.java:559)
android.view.Choreographer$FrameDisplayEventReceiver.run (Choreographer.java:774)
android.os.Handler.handleCallback (Handler.java:808)
android.os.Handler.dispatchMessage (Handler.java:103)
android.os.Looper.loop (Looper.java:193)
android.app.ActivityThread.main (ActivityThread.java:5299)
java.lang.reflect.Method.invokeNative (Method.java)
java.lang.reflect.Method.invoke (Method.java:515)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:829)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:645)
dalvik.system.NativeStart.main (NativeStart.java)
Replace
//Adapter
FirebaseRecyclerAdapter<String, MessageViewHolder> mFirebaseAdapter=
new FirebaseRecyclerAdapter<String, MessageViewHolder>(
String.class,
android.R.layout.simple_list_item_1,
MessageViewHolder.class,
myRef.child("Message")
)
with
//Adapter
FirebaseRecyclerAdapter<String, MessageViewHolder> mFirebaseAdapter=
new FirebaseRecyclerAdapter<String, MessageViewHolder>(
String.class,
android.R.layout.simple_list_item_1,
MessageViewHolder.class,
myRef
)
Replace myRef.child("Message") with myRef. Based on your onAdd method.
My First attempt at solving this problem was on concentrating on the chuck that i suspected didn't work.From the error message,
Exception
java.lang.ClassCastException:android.support.v7.widget.AppCompatTextView
cannot be cast to android.view.ViewGroup
my best guess was the RecyclerView and its coressponding FirebaseRecyclerAdapter.I replaced it with a ListView and its corresponding FirebaseListAdapter thus:
ListView ml=(ListView) findViewById(R.id.listview);
FirebaseListAdapter<String> adapter=new FirebaseListAdapter<String>(this,String.class,android.R.layout.simple_list_item_1,myRef.child("Message"))
{
@Override
protected void populateView(View v, String model, int position) {
TextView tv=(TextView) v.findViewById(android.R.id.text1) ;
tv.setText(model);
}
};
ml.setAdapter(adapter) ;
The ListView worked; thus confirming that the Firebase database was well formed.I proceeded to re-develop the app with RecyclerView and i realized the first problem (even after successfully cleaning the project) viz:android studio added a version of compile code to my build.graddle file viz: compile 'com.android.support:recyclerview-v7:25.0.0'
which was different from my sdk version 24.2.1.Hence i applied the following correction viz:
compile 'com.android.support:recyclerview-v7:24.2.1'
By and large,this correction only stopped the app from crashing but did not solve the problem of displaying my list of data.The following link solved the main problem - casting.The link is a previous question on cast-error-with-firebaserecyclerviewadapter on this platform. So i followed it and it worked.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.