Ad
Trying To Make A Chat App In Android Studio But I Am Unable To Append The Texts
Chat.java :
My app only shows the last message typed and shows in the ChatWindow. My firebase Database gets the messages but i am unable to show the same to the user in my app. I want to show at least 5-10 previous messages to the user.
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);
}
});
}
}
}
My 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>
A screentshot of the VirtualMobile: https://imgur.com/a/XUWgom7
Ad
Answer
Chat_Window.setText("");
Add this line after you initialize Chat_Window
.
Chat_Window.setText(dataSnapshot.getValue().toString());
Change this line to -
Chat_Window.append("\n" + dataSnapshot.getValue().toString());
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad