0

I have an Income model like this

@Table(name = "Income") public class Income extends Model {
    @Column(name = "AmountDate")
    public String amountDate;

    @Column(name = "Amount")
    public int amount; 

    @Column(name = "Day")
    public int day;

    @Column(name = "Month")
    public int month;

    @Column(name = "Year")
    public int year;
}

I would like to filter data from database like it is shown below. In listView month/year like this enter image description here

And on ListViewItem click i would like to display all data for selected month/year.

So, my question is how to display month/year (if data for month exists in database) in listView?

RubyDigger19
  • 835
  • 13
  • 38
  • How is that "filtering"? Seems you're just showing the data. Of course, now knowing that `amountDate` contains, it's difficult to say what you're trying to do. --- *Why* is `amountDate` a `String` and not a date type, e.g. `java.util.Date` or `java.time.LocalDate`? – Andreas Jul 19 '18 at 21:35
  • I would like to group data by month/year and when listview item is clicked then open new fragment with all data for selected month/year.. I had some trouble with dates so I decide to save it like this. – RubyDigger19 Jul 19 '18 at 22:02
  • If you would like to **group**, why did you say **filter**? Please edit question to clarify it. – Andreas Jul 19 '18 at 23:02
  • btw, using pascal case for a column's name is a bad practice – FARS Feb 14 '23 at 01:31

2 Answers2

1

you can use 2 different ways

  1. you can create a function called group and should look like code below:

    fun group(list : List<Income>) : Map<String,List<Income>> {    
        val map = HashMap<String,List<Income>>()
        list.foreach { 
            val month = getMonthOf(it.amountDate)
            if(map.contains(mounth)){
                map[month].add(it);
            } else {
                val arrayList = ArrayList<Income>()
                arrayList.add(it)
                map[month] = arrayList
            }
         }
      return map
    }
    

where getMonthOf is a method that will extract year+mouth of the income using Date class

then you can simply use RecyclerView and then create your custom list

better to use List that its items is also list. see here for this

and for parsing date see here

2.instead of saveing date in one field you can save it in three(year and mount and day) in your data base and you can simply filter your list and create map like first way I send here

so It is getMonthOf code for get unique string for every month of year:

fun getMonthOf(income:Income) = "${income.year}/${income.month}"

now you can simply send your map to your adapter and make your list

Community
  • 1
  • 1
Shayan D
  • 620
  • 3
  • 14
0

you need to create custome Listview for this kind of operation on listview,

you can refere this Link hope this will help you.

Hardik Vegad
  • 111
  • 1
  • 7