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

时间:2019-08-14 15:05:59

标签: java android firebase firebase-realtime-database chat

Chat.java:

我的应用仅显示最后键入的消息,并显示在ChatWindow中。我的Firebase数据库收到了消息,但我无法在我的应用程序中向用户显示该消息。我想向用户显示至少5-10条以前的消息。


    @Override
        public String generateInvoicePdf(Integer invoiceId, HttpServletRequest request, HttpServletResponse response) {

             //createPDF() will create a .pdf file
            createPDF(pdfFilename, dto, dtoForSupplier);
            if (pdfFilename != null) {
                try {
                    File file = new File(pdfFilename);
                    FileInputStream is = new FileInputStream(file);
                    response.setContentType("application/blob");

                    // Response header
                    response.setHeader("Pragma", "public");
                    response.setHeader("responseType", "blob");
                    response.setHeader("Content-Disposition", "attachment; filename=\"" + pdfFilename + "\"");

                    // Read from the file and write into the response
                    OutputStream os = response.getOutputStream();
                    System.out.println(os);
                    byte[] buffer = new byte[(int) file.length()];

                    int len;
                    while ((len = is.read(buffer)) != -1) {
                        os.write(buffer, 0, len);
                    }

                    System.out.println(os);
                    os.flush();
                    os.close();
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return pdfFilename;
        }

我的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的屏幕截图: https://imgur.com/a/XUWgom7

Firebase Database structure:

1 个答案:

答案 0 :(得分:0)

Chat_Window.setText("");

初始化Chat_Window后添加此行。

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

将此行更改为-

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