尝试从Firebase数据库读取时获取空值

时间:2022-07-31 21:54:25

I have a problem with reading values from the Firebase database as I seem to not recieve any values at all. I'm trying to make a simple chat app using firebase and android. It seems I can however upload data to the database just fine...The app doesn't crash, it just displays one empty textview instead of 3 textviews each containing a message. Below is my code:

我在从Firebase数据库中读取值时遇到问题,因为我似乎根本没有收到任何值。我正在尝试使用firebase和android制作一个简单的聊天应用程序。然而,我似乎可以将数据上传到数据库...应用程序不会崩溃,它只显示一个空的textview而不是每个包含一条消息的3个textviews。以下是我的代码:

MainActivity.java:

MainActivity.java:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.FirebaseApp;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class MainActivity extends AppCompatActivity {

    private EditText etMessage;
    private DatabaseReference databaseReference;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etMessage = (EditText) findViewById(R.id.etMessage);

        databaseReference = FirebaseDatabase.getInstance().getReference().child("Messages");

        recyclerView = (RecyclerView) findViewById(R.id.RVmessage);
        recyclerView.setHasFixedSize(true);

        LinearLayoutManager llm = new LinearLayoutManager(this);
        llm.setStackFromEnd(true);
        recyclerView.setLayoutManager(llm);

    }

    public void sendMessage(View v) {
        final String msgString = etMessage.getText().toString().trim();
        if(!TextUtils.isEmpty(msgString)) {
            final DatabaseReference newMsg = databaseReference.push();
            newMsg.child("content").setValue(msgString);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();

        FirebaseRecyclerAdapter <Message, MessageViewHolder> fbra = new FirebaseRecyclerAdapter<Message, MessageViewHolder>(
                Message.class,
                R.layout.singlemessagelayout,
                MessageViewHolder.class,
                databaseReference
        ) {
            @Override
            protected void populateViewHolder(MessageViewHolder viewHolder, Message model, int position) {
                viewHolder.setMsg(model.getMsg());
            }
        };
        recyclerView.setAdapter(fbra);
    }

    public static class MessageViewHolder extends RecyclerView.ViewHolder {

        View view;

        public MessageViewHolder(View itemView) {
            super(itemView);
            view = itemView;
        }

        public void setMsg(String msg) {
            TextView tvMessage = (TextView) view.findViewById(R.id.tvMessage);
            tvMessage.setText(msg);
        }
    }
}

activity_main.xml:

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.tumex.chatapp.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/linearLayout"
        android:id="@+id/RVmessage">

    </android.support.v7.widget.RecyclerView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:id="@+id/linearLayout">

        <EditText
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:id="@+id/etMessage"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Send"
            android:onClick="sendMessage"/>
    </LinearLayout>

</RelativeLayout>

Message.java:

Message.java:

public class Message {
    private String msg;

    public Message() {

    }

    public Message(String msg) {
        this.msg = msg;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

simplemessagelayout.xml (layout for a single message):

simplemessagelayout.xml(单个消息的布局):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/tvUsername"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tumex"
        android:textSize="17dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tvMessage"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:text="Hello everybody"
        android:textColor="#000000"
        android:textSize="13dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:textStyle="bold" />

</LinearLayout>

This is what my Firebase DB looks like:

这就是我的Firebase DB的样子:

尝试从Firebase数据库读取时获取空值

This is what my app looks like when I run it:

这是我的应用程序运行时的样子:

尝试从Firebase数据库读取时获取空值

What needs to happen is it needs to show 3 messages something like:

需要发生的是它需要显示3条消息:

Tumex
hi

Tumex
hi

Tumex
hi

if I need to post any gradle files please let me know and I will do so. Thank you in advance.

如果我需要发布任何gradle文件请告诉我,我会这样做。先谢谢你。

2 个解决方案

#1


0  

I've never used FirebaseRecyclerAdapter, but looking at its source code and this sample code I think you either need to pass a Lifecycle owner to its constructor (if you are using Android Architecture Components) or call its startListening/stopListening methods.

我从未使用过FirebaseRecyclerAdapter,但查看其源代码和此示例代码,我认为您需要将Lifecycle所有者传递给其构造函数(如果您使用的是Android体系结构组件)或调用其startListening / stopListening方法。

If you use the Android Architecture Components and pass the activity to the constructor, it will handle all lifecycle events (like onStart, onPause, etc...) automagically.

如果您使用Android架构组件并将活动传递给构造函数,它将自动处理所有生命周期事件(如onStart,onPause等)。

If not, then you probably need to call startListening() on onStart() and stopListening() on onStop().

如果没有,那么你可能需要在onStart()上调用startListening()并在onStop()上调用stopListening()。

#2


0  

Feeling like a dummy right now...the problem was that in my simplemessagelayout.xml the android:layout_height="match_parent"> should have been android:layout_height="wrap_content"> so the messages were there I just couldn't see them because every message had a screen-sized empty while space at the end.

现在感觉像个假人......问题是在我的simplemessagelayout.xml中,android:layout_height =“match_parent”>应该是android:layout_height =“wrap_content”>所以消息在那里我只是看不到它们因为每条消息都有一个屏幕大小为空,而末尾有空格。

#1


0  

I've never used FirebaseRecyclerAdapter, but looking at its source code and this sample code I think you either need to pass a Lifecycle owner to its constructor (if you are using Android Architecture Components) or call its startListening/stopListening methods.

我从未使用过FirebaseRecyclerAdapter,但查看其源代码和此示例代码,我认为您需要将Lifecycle所有者传递给其构造函数(如果您使用的是Android体系结构组件)或调用其startListening / stopListening方法。

If you use the Android Architecture Components and pass the activity to the constructor, it will handle all lifecycle events (like onStart, onPause, etc...) automagically.

如果您使用Android架构组件并将活动传递给构造函数,它将自动处理所有生命周期事件(如onStart,onPause等)。

If not, then you probably need to call startListening() on onStart() and stopListening() on onStop().

如果没有,那么你可能需要在onStart()上调用startListening()并在onStop()上调用stopListening()。

#2


0  

Feeling like a dummy right now...the problem was that in my simplemessagelayout.xml the android:layout_height="match_parent"> should have been android:layout_height="wrap_content"> so the messages were there I just couldn't see them because every message had a screen-sized empty while space at the end.

现在感觉像个假人......问题是在我的simplemessagelayout.xml中,android:layout_height =“match_parent”>应该是android:layout_height =“wrap_content”>所以消息在那里我只是看不到它们因为每条消息都有一个屏幕大小为空,而末尾有空格。