【发布时间】:2018-04-05 07:17:56
【问题描述】:
实际上,我正在尝试通过从服务器 (mongodb) 获取一组帖子来在 recyclerview 中显示帖子,并且工作正常。然后我试图在post_ owner_mail 的帮助下加载post's username。你可能会说为什么我要明智地加载这些东西而不是批量加载这些东西,但是因为会有不同ownerMail 的帖子所以不可能预取他们的用户名。为此,我使用了一个单独的类来从服务器加载数据并在文本视图中显示。但现在的问题是虽然textview 正在设置用户名,但并非一直在设置,并且recyclerview 也滞后。谁能帮我解决这个问题。
适配器类代码:
@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"
}
【问题讨论】:
-
使用google的paging支持库
-
@pskink 先生,我是初学者,请详细说明我该如何使用 分页库
-
参见
PagedListAdapter类 - 你应该将它用作适配器的基类
标签: android socket.io android-recyclerview