如何明智地将数据加载到 recyclerveiw 适配器位置? [英] How to load data to recyclerveiw adapter position wise?

查看:23
本文介绍了如何明智地将数据加载到 recyclerveiw 适配器位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我试图通过从服务器 (mongodb) 获取一组帖子来在 recyclerview 中显示帖子,并且它工作正常.然后我试图在 post_owner_mail 的帮助下加载 post 的用户名.你可能会说为什么我要按位置加载这些东西而不是批量加载,但因为会有使用不同的 ownerMail 发布帖子,因此无法预取他们的用户名.为此,我使用一个单独的类从服务器加载数据并在文本视图中显示.但现在的问题是虽然 textview 正在设置用户名但不是所有时间,并且 recyclerview 也滞后.谁能帮我解决这个问题.

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.

适配器类代码:

 @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());

}

加载数据类:

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();
    }
}
}

数据库存储模式:

{
"_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"
}

推荐答案

您有几个解决方案可以解决此问题:

You have a few solutions to solve this issue:

1) 您可以将电子邮件地址与您呈现给用户的项目结合起来(基本上,您正在创建一个包含旧数据 + 电子邮件的新对象,或者只是向旧对象添加另一个字段),您需要访问数据库/服务器/服务,只需添加您希望添加到每个项目的必填字段(电子邮件).

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) 如果您无权访问数据库/服务,您可以创建一个加载器,当您加载这两个项目(旧项目和电子邮件,您将已获取电子邮件)时,该加载器将呈现给用户设置适配器之前的每个项目)

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)另一种方法是根据请求显示电子邮件,只需添加一个按钮(或箭头之类的图像),当用户单击它时,它会根据您的意愿请求电子邮件并将其显示在项目中你选的.

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.

针对您的问题有不同的方法.如果显示电子邮件不重要,我会使用方法 3.

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

这篇关于如何明智地将数据加载到 recyclerveiw 适配器位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆