如何避免RecyclerView-Android中的数据重复? [英] How to avoid data repetition in RecyclerView - Android?

查看:310
本文介绍了如何避免RecyclerView-Android中的数据重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用mongodb开发墙纸应用程序.我正在从数据库中检索数据,并通过data-model类的帮助将其显示在我的recyclerView上,没有任何问题.另外,我正在使用滑动刷新布局,以允许用户刷新recyclerView以获得新数据.

I'm developing a wallpaper app using mongodb. I'm retrieving data from database and displaying it on my recyclerView by the help of a data-model class without any problem. Also I'm using swipe refresh layout to allow user for refreshing the recyclerView for new data.

但是现在的问题是如何避免数据重复并仅向用户显示新帖子.我的意思是,如果我的第一个查询中的数据库中有5张图片,我会得到5张,所以当用户再次刷新布局时,recyclerView的项目会增加到10张,我想避免这种情况.仅当db中的帖子增加到6个或更多时,才向他们显示新图片.

But now the problem is how can I avoid data repetition and show only new posts to the user. I meant if there are 5 pics are there in my db in my first query I'll get those 5 so when the user will refresh the layout again the recyclerView's item is increased to 10 and I wanna avoid this I want to show them new pics only when the posts in db will be increased to 6 or more.

我认为这种避免数据的概念也用于社交媒体应用中.但是在这种情况下,我想知道我该怎么办?

I think this data avoid concept is also used in social media apps. but for this context I wonder what I have to do?

数据模型类:

public class TimelineData {
    private String type, time, img_link;

    public TimelineData(String type, String time, String img_link) {
        this.type = type;//type means what type of wallpaper
        this.time = time;
        this.img_link = img_link;
    }


    public String getType() {
        return type;
    }

    public String getTime() {
        return time;
    }

    public String getImg_link() {
        return img_link;
    }

}

将数据添加到recyclerview:

Adding Data to recyclerview:

private List<TimelineData> timelineDataList = new ArrayList<>();

public void onCreateView() {
    recyclerview.setItemViewCacheSize(20);
    recyclerview.setDrawingCacheEnabled(true);
    recyclerview.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
    recyclerview.setLayoutManager(new LinearLayoutManager(ctx));
    //Setting Adapter
    adapter=new CustomRecyclerViewAdapter(timelineDataList);
    recyclerview.setAdapter(adapter);
}

@Override
public void onStart() {
    super.onStart();
    // Fetching data from server
    socket.disconnect();
    socket.connect();

    //Getting Data from server
    JSONObject obj=new JSONObject();
    try {
        obj.put("timeline_posts","all");
        socket.emit("data",obj);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
void addTimelineData(String type,String time,String img_link) {
    timelineDataList.add(new TimelineData(type,time,img_link));
    adapter.notifyDataSetChanged();
}

private  Emitter.Listener handlePosts = new Emitter.Listener() {

    @Override
    public void call(final Object... args){
        try {
            JSONArray jsonArray=(JSONArray)args[0];
                          for(int i=0;i<jsonArray.length();i++){
                   try {
                       JSONObject ob=jsonArray.getJSONObject(i);

                       post_type=ob.getString("post_type");


                       post_time=ob.getString("time");

                       post_link=ob.getString("img_link");

                       addTimelineData(post_type,post_time,post_link);

                   } catch (JSONException e) {
                       e.printStackTrace();
                   }
               }

        } catch (Exception e) {
            Log.e("error",e.toString());
        }
    }
};

推荐答案

无论何时获取新数据,您都可以尝试清理数据源,这样一来,您将始终重新插入完整的数据集,如果有新数据,则将其插入使用旧版本,您不必担心移动应用程序中的重复数据,而只需担心服务器中的重复数据.

You can try cleaning the data source whenever you get new data, that way you'll always reinsert the complete dataset, if you have new data it will be inserted with the old one and you don't have to worry about repeated data in the mobile app, only in the server.

 private List<TimelineData> timelineDataList=new ArrayList<>() ;
 public void onCreateView(){
    recyclerview.setLayoutManager(new LinearLayoutManager(ctx));
    //Setting Adapter
    adapter=new CustomRecyclerViewAdapter(timelineDataList);
    recyclerview.setAdapter(adapter);
  }
@Override
public void onStart() {
    super.onStart();
    // Fetching data from server
    socket.disconnect();
    socket.connect();

    //Getting Data from server
    JSONObject obj=new JSONObject();
    try {
        obj.put("timeline_posts","all");
        socket.emit("data",obj);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
void addTimelineData(String type,String time,String img_link){
     boolean isRepeated = false;
     for(TimelineData data : timelineDataList){
         if(data.getTime().equals(time)){
           isRepeated = true;
         }
     }
     if(!isRepeated){
        timelineDataList.add(new TimelineData(type,time,img_link));
     }

    adapter.notifyDataSetChanged();
}
private  Emitter.Listener handlePosts = new Emitter.Listener(){

@Override
public void call(final Object... args){
    try {
        JSONArray jsonArray=(JSONArray)args[0];
         timelineDataList.clear(); //clear data before inserting new one
                      for(int i=0;i<jsonArray.length();i++){
               try {
                   JSONObject ob=jsonArray.getJSONObject(i);

                   post_type=ob.getString("post_type");


                   post_time=ob.getString("time");

                   post_link=ob.getString("img_link");

                   addTimelineData(post_type,post_time,post_link);

               } catch (JSONException e) {
                   e.printStackTrace();
               }
           }

    } catch (Exception e) {
        Log.e("error",e.toString());
    }
}
};

这篇关于如何避免RecyclerView-Android中的数据重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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