3

I will try to be brief:

I have three classes that have to interact in this:

  1. The server, which receives the message (in a thread).
  2. The contact, which stores messages in its class (in each object).
  3. The chat activity belonging to each user, which has to show the messages of the corresponding object, the idea is that it is a RecyclerView.(an activity)

The server receives a message, this message would be added to the chat of the specific contact and then the RecyclerView must be updated.

This is the basic Contact class code:

public class Contact implements Serializable {

 private String name;
 private String ip;
 //Here would be a variable that contains the chat strings

 public Contact (String name, String ip){
    this.name = name;
    this.ip = ip;
 }

 public String getName() {
    return name;
 }

 public void setName(String name) {
    this.name = name;
 }

 public String getIp() {
    return ip;
 }

 public void setIp(String ip) {
    this.ip = ip;
 }
}

And then there is the activity of the chat that is launched when selecting a specific user and it contains a RecyclerView where I want the messages to be.

My problem is that, how to make the messages that are stored in the variable of a specific object appear in the list.

From already thank you very much.

mndv
  • 55
  • 6

2 Answers2

1

you can use data class instead of serialization

public class Data {

private String text;
private String uid;
private String tnviews;
private String numberlikes;
public Data(String text,String tnviews,String numberlikes,String uid) {
    this.text = text;
    this.tnviews = tnviews;
    this.numberlikes = numberlikes;
    this.uid = uid;
}

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}
/////////// VIEWS
public String getViews() {
    return tnviews;
}

public void setViews(String tnviews) {
    this.tnviews = tnviews;
}
////////     likes
public String getLikes() {
    return numberlikes;
}

public void setLikes(String numberlikes) {
    this.numberlikes = numberlikes;
}

public String getUid() {
    return uid;
}

public void setUid(String uid) {
    this.uid = uid;
}
kbhaskar
  • 139
  • 7
  • I need to use serialization to transport the data over the network. Is it possible with the Data class? And how would the RecyclerView be updated in that code? – mndv May 07 '18 at 04:04
  • watch first https://www.youtube.com/watch?v=LqBlYJTfLP4 and get the sample code https://www.androidhive.info/2016/01/android-working-with-recycler-view/ – kbhaskar May 07 '18 at 06:49
0

you can Update RecyclerView Adapter Data

mAdapter.notifyDataSetChanged();

Reference link

Community
  • 1
  • 1
Arbaz.in
  • 1,164
  • 1
  • 14
  • 33
  • Yes, I know the method, but I must do it from another thread. The server side receives a message, and then I add this message in a variable of a contact object to store all the chat there. How can I get it in this case? Or what would be the optimal way to do it by storing a chat for each user? – mndv May 07 '18 at 22:33