Detecting the previous item in recycler view

Viewed 2839

I have a model class called (messages). In on bind view holder method of recycler view adapter I do this to extract the (time) string from the (messages) class:

@override
public void onBindViewHolder(final ViewHodler view holder, int position){

  //here I extract the time value from (messages) class

  messages mess=messageslist.get(position);

  //I store it in a long variable called time

  long time= mess.getTime();


 }

Until here I am only getting the time value of each item in recycler view. But I want also to get the time of the previous item and compare it to the next one.

In other words I am trying to find the difference of time between each 2 items in recyclerview.

Thanks for your help.

EDIT

okay what I want to do is to show or hide a date view if the difference of date between the 2 items reached or passed 1 day. If I do it according to the answers below I get no results...like this

    //the time of the item
    long time= mess.getTime();

     //the time of the previous item according to the answers below

    long pre_time=messageslist.get(position-1).getTime();


     //now I compare the difference 


      if(time - pre_time >= 24*3600*1000){ 

         //show date
       }else{
         //hide date
        }

but doest work.

3 Answers

Edited the answer based on updated question

You should convert the time milliseconds to DAY_OF_MONTH and check if the day of the current item is greater than the day of the previous item.

Calendar calendar = Calendar.getInstance();

Messages mess = messagesList.get(position);
calendar.setTimeInMillis(mess.getTime());
int date = calendar.get(Calendar.DAY_OF_MONTH);

if (position > 0) {
    Messages previousMessage = messagesList.get(position - 1);
    if (previousMessage != null) {
        calendar.setTimeInMillis(previousMessage.getTime());
        int prevDate = calendar.get(Calendar.DAY_OF_MONTH);

        if (date > prevDate) {
            // This is a different day than the previous message, so show the Date
        } else {
            // Same day, so hide the Date
        }

    }
} else {
    // This is the first message, so show the date
}
messages mess = messageslist.get(position);
if (position > 0){
    long time_diffrence = mess.getTime() - messageslist.get(position-1).getTime();
}

(1) Step one timestamp to Date convert menthod

private String getDate(String createdDate) {
    long timestampString = Long.parseLong(createdDate);
    @SuppressLint("SimpleDateFormat") String value = new java.text.SimpleDateFormat("dd/MM/yyyy").
            format(new java.util.Date(timestampString * 1000));
    return value;
}

(2) Step two in onBindViewHolder

    if (position == 0) {
        holder.mTextViewNewTime.setVisibility(View.VISIBLE);
        holder.mTextViewNewTime.setText(getDate(bean.getCreatedDate()));
    } else {
        String prev_date_time = getDate(mPojoTicketChatList.get(position - 1).getCreatedDate());
        //String prev_date[] = prev_date_time[0].split("-");
        if (getDate(bean.getCreatedDate()).equals(prev_date_time)) {
            holder.mTextViewNewTime.setVisibility(View.GONE);
        } else {
            holder.mTextViewNewTime.setVisibility(View.VISIBLE);
            holder.mTextViewNewTime.setText(getDate(bean.getCreatedDate()));
        }
    }

Try it working for me

Related