Answer the question
In order to leave comments, you need to log in
Not displaying TextView text correctly via ArrayAdapter?
Made it so that each incoming message is written to its own TextView.
This method is called on the thread.
public void inputMessage(){
try {
InputStream inputStream = socket.getInputStream();
DataInputStream dataInputStream = new DataInputStream(inputStream);
String stringInputMessage;
while (true){
if ((stringInputMessage = dataInputStream.readUTF()) != null){
final String finalStringInputMessage = stringInputMessage;
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView textViewMessage = createTextView();
textViewMessage.setText(finalStringInputMessage);
listMessage.add(textViewMessage);
listViewMessage.setAdapter(arrayAdapterMessage);
}
});
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private TextView createTextView() {
textViewMessage = new TextView(this);
return textViewMessage;
}
Answer the question
In order to leave comments, you need to log in
The problem is with the adapter.
No need to create a new TextView for new messages, just add the message to the list of all messages and pass that list to the adapter to update the ListView (in this case, get a reusable TextView out of the box):
List<String> messages = new ArrasyList<>();
//when new message arrives
messages.add(msg);
//update adapter
arrayAdapterMessage.setMessages(messages) ;
arrayAdapterMessage.notifyDatasetChanged();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question