困难,获得实时 RecyclerView FirebaseUI android 显示来自数据库的消息

标签: Java Android
发布时间: 2017/3/19 17:48:43
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我已经经历了困难,无法得到我 Android 应用程序的 RecyclerView 来填充 FirebaseUI adapter 与数据已经在 Firebase 数据库,虽然我可以将数据发送到它使用︰

public void onAdd(View v){
    //create Firebase Object
    t=(EditText) findViewById(R.id.display);
    String mitems=t.getText().toString();
    myRef.push().setValue(mitems.toString());
}

我用以下接收我发送的数据的目的,但它不工作︰

@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);
}

我 ViewHolder 是这样定义的︰

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>

这里是错误消息︰

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)

解决方法 1:

我第一次尝试解决这一问题是集中在夹头上,我怀疑没有工作。从该错误消息,

Exception 
java.lang.ClassCastException:android.support.v7.widget.AppCompatTextView 
cannot be cast to android.view.ViewGroup 

我最好的猜测是 RecyclerView 和 FirebaseRecyclerAdapter.I 换上了其小学语文 ListView 和其相应的 FirebaseListAdapter 因此︰

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) ;

ListView工作; 因此确认 Firebase 数据库也成立。我着手重新开发的应用程序与 RecyclerView 和我意识到的第一个问题 (即使在之后成功地清洗工程) 即︰ android studio即添加到我的 build.graddle 文件版本的编译代码︰ compile 'com.android.support:recyclerview-v7:25.0.0' 是有别于我的 sdk 版本 24.2.1.Hence 即应用以下修正︰

compile 'com.android.support:recyclerview-v7:24.2.1'

大体上,这种修正只停止该应用程序从崩溃,但并没有解决问题的显示我的列表的数据。下面的链接解决的主要问题-铸造。链接是前一个问题上铸-错误-与-firebaserecyclerviewadapter 在这个平台上。于是我跟着它,和它的工作。

官方微信
官方QQ群
31647020