简体   繁体   English

如何避免RecyclerView中的数据重复 - Android?

[英]How to avoid data repetition in RecyclerView - Android?

I'm developing a wallpaper app using mongodb . 我正在使用mongodb开发一个壁纸应用程序。 I'm retrieving data from database and displaying it on my recyclerView by the help of a data-model class without any problem. 我正在从数据库中检索数据,并在data-model类的帮助下将其显示在我的recyclerView ,没有任何问题。 Also I'm using swipe refresh layout to allow user for refreshing the recyclerView for new data. 此外,我正在使用滑动刷新布局,以允许用户刷新recyclerView以获取新数据。

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. 我的意思是如果在我的第一个查询中我的数据库中有5个图片我会得到那些5因此当用户再次刷新布局时, recyclerView的项目增加到10并且我想避免这个我想要显示它们只有当db中的帖子增加到6或更多时才会出现新的图片。

I think this data avoid concept is also used in social media apps. 我认为这种数据避免概念也用于社交媒体应用程序。 but for this context I wonder what I have to do? 但对于这种情况,我想知道我该做什么?

Data model class: 数据模型类:

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

}

Adding Data to recyclerview: 将数据添加到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());
    }
}
};

before you add new elements to the wallpaper list, check to see if an object with that id exist in the list. 在将新元素添加到壁纸列表之前,请检查列表中是否存在具有该ID的对象。 if it does, skip it, else add it. 如果是,请跳过它,否则添加它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM