java - 尝试在 android studio 中制作一个聊天应用程序,但我无法附加文本

标签 java android firebase firebase-realtime-database chat

聊天.java:

我的应用程序仅显示最后输入的消息并显示在聊天窗口中。我的 firebase 数据库收到消息,但我无法在我的应用程序中向用户显示相同的消息。我想向用户显示至少 5-10 条之前的消息。

    public class Chat extends AppCompatActivity {
    private DatabaseReference Database;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        final UserLocalStore localStore = new UserLocalStore(Chat.this);
        Database = FirebaseDatabase.getInstance().getReference("Messages");
        final TextView Chat_Window=(TextView)findViewById(R.id.chat_window);
        Database.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @androidx.annotation.Nullable String s) {
                Log.e("DatasnapShot",dataSnapshot.getKey().toString());

                Chat_Window.setText(dataSnapshot.getValue().toString());
            }
            @Override
            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @androidx.annotation.Nullable String s) {

            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @androidx.annotation.Nullable String s) {

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

        Button SendMessage = (Button)findViewById(R.id.send);
        SendMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String messageText = Chat_Window.getText().toString();            
         Database.push().setValue((localStore.getUserData().get(0).toString()));
                Database.push().setValue(ChatType.getText().toString());
                Chat_Window.append(ChatType.getText().toString());
                ChatType.setText("");
            }
        });
            Button Signout = (Button)findViewById(R.id.signout);
    {
        Signout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            UserLocalStore localStore=new UserLocalStore(Chat.this);
            localStore.LogOut();
            FirebaseAuth.getInstance().signOut();
            Intent intent = new Intent(Chat.this,MainActivity.class);
                startActivity(intent);
            }
        });
    } 
    }
}

我的activity_chat.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.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:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <EditText
    android:id="@+id/chat_type"
    android:layout_width="260dp"
    android:layout_height="44dp"
    android:layout_marginBottom="2dp"
    android:ems="10"
    android:hint="Enter message to send..."
    android:inputType="textPersonName"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

   <Button
    android:id="@+id/send"
    android:layout_width="122dp"
    android:layout_height="47dp"
    android:layout_marginStart="29dp"
    android:text="Send"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toEndOf="@+id/chat_type" />

    <TextView
    android:id="@+id/chat_window"
    android:layout_width="406dp"
    android:layout_height="566dp"
    android:layout_marginTop="24dp"
    android:layout_marginBottom="8dp"
    android:gravity="bottom"
    android:hint="Chat message wil appear here..."
    android:padding="5dp"
    app:layout_constraintBottom_toTopOf="@+id/chat_type"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/signout" />

    <Button
    android:id="@+id/signout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="324dp"
    android:text="Sign Out"
    app:layout_constraintBottom_toTopOf="@+id/chat_window"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@id/chat_window" />

    </androidx.constraintlayout.widget.ConstraintLayout>

VirtualMobile 的屏幕截图: /image/Plciy.jpg

Firebase Database structure:

最佳答案

Chat_Window.setText("");

初始化 Chat_Window 后添加此行。

Chat_Window.setText(dataSnapshot.getValue().toString());

将此行更改为 -

Chat_Window.append("\n" + dataSnapshot.getValue().toString());

关于java - 尝试在 android studio 中制作一个聊天应用程序,但我无法附加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57497403/

相关文章:

java - Travis CI 上的 Gradle 构建失败

java - 向数据库提交信息后刷新java程序

java - 无法读取架构文档 'http://www.springframework.org/schema/security/spring-security-4.0.xsd'

Android - 使用 google maps api 动态更改标记的位置

java - Android将单个.java文件编译为.class

firebase - Flutter-了解Firebase Admin以及如何从电子邮件/uid/名称获取用户信息

spring - 如何修复此 FirebaseApp 名称 [DEFAULT] 已存在! spring-boot 和 firebase

java - 当数据源是 SQLContainer 时如何使只有一列可编辑

android.mk LOCAL_MODULE_FILENAME 不应包含扩展名

Firebase 云函数 -@google-cloud/storage 初始化