0

I want to list my data in this way in android in recycler view how can I move on with this. How to divide data on the basis of date.

and data is in this form

[
{
"type": "IMPORTED",
"title": "Google Cal X",
"datetime": 1541340000000
},
{
"type": "IMPORTED",
"title": "Google Cal Z",
"datetime": 1541343600000
},
{
"type": "HOLIDAY",
"title": "Summer Bank Holiday 3"
},
{
"type": "HOLIDAY",
"title": "Summer Bank Holiday 4"
}
]

Please help me out.

Ali
  • 3,346
  • 4
  • 21
  • 56
  • you make custom logic into onBindView method into adapter class. Add some check based on date. –  Dec 03 '18 at 07:10
  • Possible duplicate of [Divide elements on groups in RecyclerView](https://stackoverflow.com/questions/34848401/divide-elements-on-groups-in-recyclerview) – Rajen Raiyarela Dec 03 '18 at 07:12
  • Yes I get that but I am not able to divide data on the date basis because each child has their own data so in recycler view its printing again and again – Divyank Vijayvergiya Dec 03 '18 at 07:13
  • You can create HashMap> where string will be your date and YourModelClass contains other information and you can bind with RecyclerView (Show Date) (Nested RecyclerView (Show Custom Data) or You can use ExpandableListView – Ketan Ramani Dec 03 '18 at 07:14

1 Answers1

1

Either go with the solution provided by Ketan Ramani or follow this simple trick in onBindViewHolder()

Note: to follow my answer, first your JSON should be sorted from the backend according to date and time like new Items should go first in the list

First, make a ModelClass for your data, for example, you have three things in your JSON type, title, DateTime, so your model class for this will look like this,

public class ModelClass{
public String title;
public String type;
public String dateTime;

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public String getDateTime() {
    return dateTime;
}

public void setDateTime(String dateTime) {
    this.dateTime = dateTime;


 }
}

After this just go to your single_item.xml view in the layout directory and add a textView to show date at the top of the view just like the show in your reference image and set its VISIBILITY to GONE

Now in onBindViewHolder() simple match the current date in the list with next position date using getter-setter like this,

if (position < notificationListData.size()) {      
String previousDateTime = (((ModelClass) notificationListData.get(position)).getDateTime());
            if(previousDateTime.equals((((ModelClass) notificationListData.get(position+1)).getDateTime()))){
                hide the textView containing date;
            }else{
                show text view containing Date  and setDate on that textView
            }
}

Let me know if this was helpful.

rajat singh
  • 165
  • 1
  • 11