【问题标题】:Data failing to display in Firebase Recycler View数据无法在 Firebase Recycler 视图中显示
【发布时间】:2017-10-28 16:38:04
【问题描述】:

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

两个主要的:

Herehere

我在 Firebase Recycler 视图中使用 Message 对象(包括在下面)。由于某种原因,数据未显示。从我的日志输出来看,适配器中的任何内容都没有被调用。这是位于我的 onCreate() 中的活动代码:

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

    }

这是我的 ViewHolder:

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 和适配器的 Message 对象(我输入的数据同步到 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_message_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_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 >

【问题讨论】:

  • 对于未来的访问者,您可以查看 here,其中我已经逐步解释了如何使用 Android 将 Firestore 中的数据显示到 RecyclerView

标签: android firebase android-recyclerview firebaseui


【解决方案1】:

有两件事需要解决。

1) 添加adapter.startListening();(如 Elvin No Matter 所述)。就我而言,我在 onCreate 末尾附近的 mRecyclerView.setAdapter(adapter); 下方添加了这个。

2) 将我的视图布局 XML 包含在 &lt;android.support.v7.widget.LinearLayoutCompat&gt; 中。查看我的新视图持有者,对按钮进行了一些其他不相关的更改:

<?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】:

    根据readme

    FirestoreRecyclerAdapter 使用快照监听器来监控 Firestore 查询的更改。要开始侦听数据,请调用 startListening() 方法。你可能想在你的 onStart() 中调用它 方法。确保您已完成所有必要的身份验证 在调用 startListening() 之前读取数据,否则您的查询将失败。

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

    @Override
    protected void onStop() {
        super.onStop();
        adapter.stopListening();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多