How to load data to recyclerveiw adapter position

2019-07-26 06:18发布

问题:

Actually I'm trying to show posts in recyclerview by getting a set of posts from server (mongodb) and it's working fine. Then I'm trying to load post's username by the help of post_ owner_mail .You might say why do I load these things position wise instead of bulk wise but as there will be posts with different ownerMail so it's not possible to prefetch their username. For that purpose I'm using a separate class for loading data from server and showing in text view . But now the problem is the although textview is setting username but not all time and the recyclerview is lagging as well . Can anyone please help me to get rid of this problem.

Adapter Class Code :

 @Override
public void onBindViewHolder( CustomRecyclerViewHolder holder, int position) {
  //It's a data model class of image_links and post_owner_mails
  TimelineData timelineData=totalList.get(holder.getAdapterPosition());

 //It's the custom loader class through which I'm loading data and setting to txt view position wise
  LoadData d=new LoadData(hold.userNameTxtView,timelineData.getPostOwnerMail());

}

LoadData Class :

public class LoadData{
private Socket socket;
{
    try{
        socket = IO.socket("http://192.168.43.218:8080");
    }catch(URISyntaxException e){
        throw new RuntimeException(e);
    }
}
public LoadData(final TextView tv,final String email){
    socket.disconnect();
    socket.connect();
    JSONObject ob=new JSONObject();
    try {
        ob.put("getFullnameMail",email);
        socket.emit("data",ob);
        socket.on("fullname", new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                final JSONObject ob=(JSONObject)args[0];
                Needle.onMainThread().execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            tv.setText(ob.getString("fullname"));
                            socket.disconnect();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }    
                    }
                });
            }
        });
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
}

Database Storing Pattern:

{
"_id" : ObjectId("5aa9304391b4ee28f4e4525d"),
"post_owner_mail" : "john.cena",
"time" : "14 Mar 2018(01-30-50)",
"img_link" : [ 
    "192.168.43.218:8080/user_info/john.cena/uploads/14 Mar 2018(01-30-50)/pic0.jpg", 
    "192.168.43.218:8080/user_info/john.cena/uploads/14 Mar 2018(01-30-50)/pic1.jpg"
],
"caption" : "#fun#cartoon"
}

回答1:

You have a few solutions to solve this issue:

1) you can combine the email address with the item that you are presenting to the user (basically you are creating a new object that contain the old data + the email, or just add another field to the old object), you require to have access to the database/server/service and just add the required field (email) that you are looking to add to each item.

2) if you don't have access to the database/service you can create a loader that will be presented to the user while you are loading those two items(old item and the email, you will have email fetched already for each item before you set your adapter)

3) a different approach would be to show the email upon request, just add a button(or image like arrow) and when the user clicks on it, it will request the email as you wished and present it in the item that you picked.

There are different approaches for your problem. If it's not important to show the email I would go with method 3.