简体   繁体   中英

How to avoid data repetition in RecyclerView - Android?

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.

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?

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:

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. if it does, skip it, else add it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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