How to marked as "new" to newest items in the recycler view in anroid?

Viewed 96

I have items to show in android app via recycler view. I have successfully implemented and fetch the data from server. Items are showing in the recycler view list. I have also sorted the newest items in the top of the list. I have done in the backend to sort the newest items in the top of the list by using php code and SQL and successfully done it. But now I want to implement a text that is this:- NEW in the list. Which means it will only show for the newest items and automatically deleted after 5 days.

I have successfully implement the NEW tag in the frontend and it is showing, But The new tag is only showing for the current date. I want to show that "NEW" tag for 5 more days. I put logic but it is not helping me, and it is showing for only for the current date. Here is my code for showing NEW:-

MyAdapter class --> onBindViewHolder()

Json data:-
"creation_date": "2021-09-06"

final String creation_date = ModelClass.getCreation_date(); //JSON data "creation date". getCreation_date() invoking Getter method from model class"

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

Calendar c = Calendar.getInstance();

String Current_date = sdf.format(c.getTime());

c.add(Calendar.DAY_OF_MONTH, 5); //here I have increase the date to 5
String EndDate = sdf.format(c.getTime());


 if (Current_date.equals(creation_date)) {
                        myViewHolder.creation_date.setVisibility(View.VISIBLE);
                    }

                     else if (creation_date.equals(EndDate)) {
                        myViewHolder.creation_date.setVisibility(View.VISIBLE);
                    } else {
                        myViewHolder.creation_date.setVisibility(View.GONE);
                    }

The main problem is How can I show the NEW tag after the creation date for 5 more days and after 5 days Textview(creation_date) Visibility will be GONE?

right now if my creation date is same with the current date then it is showing the new tag, but I want to show for 5 more days and after 5 days it should deleted.

Right now the Output is showing like this:-

Sample Image

2 Answers

This Answer will help you. Try it, If you face any error let me know... I have use your above sample code. Here is my version of code:


final String creation_date = storiesModel.getCreation_date();

                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

                    SimpleDateFormat Cussdf = new SimpleDateFormat("yyyy-MM-dd");

                    Calendar c = Calendar.getInstance();

                    //new
                    Calendar cust = Calendar.getInstance();

                    try {
                        cust.setTime(Cussdf.parse(creation_date));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

                    cust.add(Calendar.DAY_OF_MONTH, 1);
                    String customIncreDate = Cussdf.format(cust.getTime());
                    System.out.println("Your custom Increment creation date is: "+customIncreDate);


                    //another one

                    Calendar customDate = Calendar.getInstance();

                    try {
                        customDate.setTime(Cussdf.parse(creation_date));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

                    customDate.add(Calendar.DAY_OF_MONTH,2); //for next increment create all "//another one" whole code and increase 2 to 3 for next 3 days
                    String CustomInDate = Cussdf.format(customDate.getTime());
                    System.out.println("Your 2nd custom Increment creation date is: "+CustomInDate);

                    //another one

                    //new

                    String Current_date = sdf.format(c.getTime());

                    c.add(Calendar.DAY_OF_MONTH, 1);//this is increment the current date only
                    String EndDate = sdf.format(c.getTime());


                    if (Current_date.equals(creation_date)) {

                        myStoryViewHolder.creation_date.setVisibility(View.VISIBLE);
                    }
                    else if (Current_date.equals(customIncreDate)){ //It is showing!!
                        myStoryViewHolder.creation_date.setVisibility(View.VISIBLE);
                    }

                     else if (Current_date.equals(CustomInDate)) {
                        myStoryViewHolder.creation_date.setVisibility(View.VISIBLE);
                    } else {
                        myStoryViewHolder.creation_date.setVisibility(View.GONE);
                    }


Calendar c = Calendar.getInstance(); this will help to create an instance of the Calendar and then we will use add() method to add days in given date string. like this:- c.add(Calendar.DAY_OF_MONTH, 1)

To increase date in the calendar instance, I took help from this site:- https://beginnersbook.com/2017/10/java-add-days-to-date/

Simply put the object for "NEW" keyword in your backend while you are creating new items. Then check it on frontend side in java or whatever you are using. You will have the item layout for your adapter in which data are being populated, so there will be a layout for "NEW" items as well which is invisible for normal items. So when you find the keyword "NEW", you can show that layout showing "NEW" keyword which was invisible till now.

That's for frontend side.

Now comes your delete logic.

If you want to delete items after 5 days, then put that time duration in the items which you want to get deleted after 5 days time. Validate items when user opens your app next time, If the time you have given is laready gone, then delete that item from the arraylist or you can delete it from the server if you have delete API.

Tell me if you have any confusion, otherwise it's good to go.

Related