0

I have Expense entity with day of the month and I want to use that to grouped by day in recycler view items. In every single item I want to have day on the top of the item and below all objects created during that day. I'm using the Room. But is it possible to do it with one entity without parent class?

Expense:

@Entity(tableName = "expense_table")
public class Expense  {

@PrimaryKey(autoGenerate = true)
private int expenseId;
private String note;
private Double value;
private String type;
private Long dateLong = System.currentTimeMillis();
private String date = new SimpleDateFormat("MM/yyyy").format(new Date(dateLong));
private static Calendar cal = Calendar.getInstance();
private int month = cal.get(Calendar.MONTH);
private int week = cal.get(Calendar.WEEK_OF_YEAR);
private int day = cal.get(Calendar.DAY_OF_MONTH);
private int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
private String weekDay = new DateFormatSymbols().getWeekdays()[dayOfWeek];
beginner992
  • 659
  • 1
  • 9
  • 28
  • You should ask how to group items in recycle view, showing the data structure doesn't add that much. – Haroun Hajem Mar 12 '19 at 23:16
  • Didn't find a native way of doing this ether you do it manually, build viewholder, adpaters and so on, or you find a library that does the sectioning for you. Also it sound like you want expendable sections, that's another problem. – Haroun Hajem Mar 12 '19 at 23:27

2 Answers2

0

Try to use the Room query to do something like this:

@get:Query("SELECT * FROM expense_table ORDER BY day DESC")
val all: LiveData<List<Expense>>
Bruno Martins
  • 1,347
  • 2
  • 11
  • 32
  • Ok, it looks interesting. But how to create expandable item? – beginner992 Mar 12 '19 at 23:11
  • Where is the GROUP BY query? – Haroun Hajem Mar 12 '19 at 23:15
  • 1
    Do you mean, create a recyclerview with section separated by date? Like this e.g: https://stackoverflow.com/a/35820668/5314844 – Bruno Martins Mar 12 '19 at 23:15
  • Good, didn't find a native way of doing sections, besides doing it manually. – Haroun Hajem Mar 12 '19 at 23:22
  • 1
    I don't think there's a native way of doing it, in this case. Sometimes you can find a recommendation on Google UI guide without a native tool to implement that. But great open source solutions are available. – Bruno Martins Mar 12 '19 at 23:32
  • Post a recommendation, and we will mark it as accepted answer so we can close this question. Also could be "expandable", since beginner992 asked for it in the comments. – Haroun Hajem Mar 12 '19 at 23:35
  • https://github.com/zhukic/Sectioned-RecyclerView https://github.com/luizgrp/SectionedRecyclerViewAdapter – Bruno Martins Mar 12 '19 at 23:40
  • @HarounHajem The GROUP BY query is probably good solution, but I need to find expandable item with only one list. I know how to group items if I have 2 lists. – beginner992 Mar 13 '19 at 11:40
  • @BrunoDiegoMartins Yes. Items separated by day, but in your example are two objects, I have only one and this is my problem... There is possible to create in room parent class, to handle list of my current class without saving it? I mean in my app I can save new expense, but above expense there is no any user or something like that because I don't need it. I think good solution is have class to handle Expense list without saving new object in room database. I hope my explanation is fine. – beginner992 Mar 13 '19 at 12:01
  • @beginner992 It can be done in a listview or an recycleview, I've seen it done before. The key in succeeding is to use two list-items. One Day-listitem and one Expense-listItem. The day-listitem is an expandable, containing expense-listItem as an subitem. But i think youll have to use two lists. Why can't you do that? – Haroun Hajem Mar 13 '19 at 23:11
  • @HarounHajem I think your solution is the best. The problem of that solution is that I don't know how to create parent automatic class, because I want to create Expense, but without creating other objects. So the Date/Day object should download current date data yourself during the creating new Expense. I'm fresh in Room and this is problem for me. Maybe you know something about that? – beginner992 Mar 13 '19 at 23:27
  • How Room retrieves data is not my expertise. Could LiveData with Room be something you are looking for? – Haroun Hajem Mar 15 '19 at 10:08
0

You will have to do some querying with GROUP BY day when you access the data. Here is a solution using 3D-party library, Sticky Headers in a RecycleView, for then displaying the groups.

Haroun Hajem
  • 5,223
  • 3
  • 26
  • 39