代码之家  ›  专栏  ›  技术社区  ›  Christopher Mills

无法在Firebase回收器视图中显示数据

  •  1
  • Christopher Mills  · 技术社区  · 8 年前

    我遵循了几个不同的文档来整理我的第一个Firebase回收器视图:

    主要有两个方面:

    Here here .

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_message_details);
    
    
            RecyclerView mRecyclerView;
            mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
            mRecyclerView.setHasFixedSize(true);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    
            Query query = myMessagesRef.child(RoomID)
                    .orderByKey()
                    .limitToLast(50);
    
            FirebaseRecyclerOptions<Message> options =
                        new FirebaseRecyclerOptions.Builder<Message>()
                                .setQuery(query, Message.class)
                                .build();
    
            FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Message, ChatHolder>(options) {
    
    
                @Override
                public ChatHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                    // Create a new instance of the ViewHolder, in this case we are using a custom
                    // layout called R.layout.message for each item
                    final View view = LayoutInflater.from(parent.getContext())
                            .inflate(R.layout.recycler_item, parent, false);
    
                    return new ChatHolder(view);
                }
    
                @Override
                protected void onBindViewHolder(ChatHolder holder, int position, Message model) {
                    //super.onBindViewHolder(holder, position);
                    Log.w(TAG, "Some Info on messages 2 " + model.MessageText);
                    holder.bindChat(model);
    
                }
    
    
    
            };
            mRecyclerView.setAdapter(adapter);
    
        }
    

    这是我的取景夹:

    static class ChatHolder extends RecyclerView.ViewHolder{
        TextView mtext;
        //Context mContext;
    
    
        public ChatHolder(View v) {
            super(v);
            mtext = (TextView) v.findViewById(com.example.administrationuser.piclo.R.id.textView13);
        }
    
        public void bindChat(Message mess){
            mtext.setText(mess.MessageText);
        }
    
    
    }
    

    这是我传递给Firebase和适配器的消息对象(我输入的数据与Firebase同步,没有问题,我对Firebase的查询以正确的格式获取数据):

    public static class Message {
    
        public String UserID;
        public String UserName;
        public String MessageText;
    
        public Message() {}  // Needed for Firebase
    
        public Message(String UserID, String UserName, String MessageText) {
            this.UserID = UserID;
            this.UserName = UserName;
            this.MessageText = MessageText;
        }
    }
    

    这是我的活动布局XML(名称:Activity\u message\u details.XML): (请注意底部附近的回收器视图):

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.example.administrationuser.piclo.MessageDetails">
    
        <LinearLayout
            android:layout_width="395dp"
            android:layout_height="643dp"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:orientation="vertical"
            tools:layout_editor_absoluteY="8dp"
            tools:layout_editor_absoluteX="8dp">
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="150dp"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
                <EditText
                    android:id="@+id/editText3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:inputType="textPersonName"
                    android:text="Name"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    
                <Button
                    android:id="@+id/button9"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:text="Button"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior"
                    android:onClick="onClick"/>
            </RelativeLayout>
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawSelectorOnTop="false"
                />
    
    
    
        </LinearLayout>
    
    
    
    
    </android.support.constraint.ConstraintLayout>
    

    最后,这里是我的视图布局XML(名称:recycler\u item.XML):

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/textView13"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TextView" />
    
    </FrameLayout >
    
    2 回复  |  直到 8 年前
        1
  •  3
  •   Christopher Mills    8 年前

    有两件事需要解决。

    1) 添加到 adapter.startListening(); (如Elvin所述,无所谓)。在我的例子中,我在 mRecyclerView.setAdapter(adapter); 我的onCreate快结束了。

    <android.support.v7.widget.LinearLayoutCompat> . 查看我的新视图支架,并对按钮进行一些其他不相关的更改:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.LinearLayoutCompat
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
     >
    
        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp">
    
            <TextView
                android:id="@+id/textView13"
                android:layout_width="285dp"
                android:layout_height="24dp"
                android:text="TextView"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                tools:layout_editor_absoluteX="-6dp" />
    
            <TextView
                android:id="@+id/textView14"
                android:layout_width="123dp"
                android:layout_height="24dp"
                android:layout_alignParentTop="true"
                android:layout_marginRight="8dp"
                android:text="TextView"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintRight_toRightOf="@+id/textView13"
                app:layout_constraintTop_toTopOf="parent"
                tools:layout_editor_absoluteX="154dp" />
    
        </android.support.constraint.ConstraintLayout>
    
    </android.support.v7.widget.LinearLayoutCompat>
    
        2
  •  1
  •   Elvin No Matter    8 年前

    根据 readme

    FirestoreRecyclerAdapter使用快照侦听器进行监视 Firestore查询的更改。要开始侦听数据,请调用 startListening()方法。您可能希望在onStart()中调用此功能 方法确保您已完成任何必要的身份验证

    @Override
    protected void onStart() {
        super.onStart();
        adapter.startListening();
    }
    

    @Override
    protected void onStop() {
        super.onStop();
        adapter.stopListening();
    }
    
    推荐文章