Android ListView with “tabs”

83 Views Asked by At

picture from a reddit news feed

(https://i.stack.imgur.com/6YXMK.jpg)

I am creating an app with a list view that is populated from a sqlite database. Each of the data base items can have a status of either “resolved” or “unresolved”.

I want the listview to have 3 “tabs” with the labels “all items”, “resolved items”, and “unresolved items” with correspoding sqlite queries to populate each.

It should behave similarly to the one pictured.

I assumed this would be a tabbed listview and have been watching tutorials for a week based on those search words and it’s taking taking me down a dark rabbit hole of fragments and changing gradles and so on. I’m not sure tabs are what i really want.

Could I do this with three buttons instead where each button would run a different query and populate my listviewcontainer?

Ideally, when the page is opened, the first “tab” would be highlighted and the listview populated with all records. As the other tabs are pressed, they would highlight and a new query would run.

Would another approach work better?

I’m not asking for code, I just want some conceptual direction on where to focus my research.

1

There are 1 best solutions below

2
Blind Kai On

If I get you right you need to filter your query results in different lists. Making a lot of queries into database is not the thing that is preferable specially if it's going to be a long process and doing it a lot of times is time and memory consuming. So to make it work you could simple store your full query result in one variable and change the RecyclerView data using custom method setList() and later using notifyDataSetChanged() to apply the changes.

To make it work you need to get understanding of "how RecyclerView works" and then you will be fine.

So after providing the right logic you would be able to simple split your whole query result as it's needed (by element values for example) as it's showed above:

About the code below:

  • list - is your query result
  • leftFilterList or rightFilterList - are lists that contain sorted items
  • adapter.setList(rightFilterList) - sets the RecyclerView data (filtered items in our case)
  • adapter.notifyDataSetChanged() - is used to notify RecyclerView that list was changed, and he need to rebuild it.

So we have two Buttons and logic that fillter items in differend ways.

public void left(View view) {
    ArrayList<ExampleItem> leftFilterList = new ArrayList<>();
    for (ExampleItem item : list) {
        if (item.getTitle().length() % 2 == 0) {
            leftFilterList.add(item);
        }
    }
    adapter.setList(leftFilterList);
    adapter.notifyDataSetChanged();
}

public void right(View view) {
    ArrayList<ExampleItem> rightFilterList = new ArrayList<>();
    for (ExampleItem item : list) {
        if (item.getTitle().length() % 2 == 1) {
            rightFilterList.add(item);
        }
    }
    adapter.setList(rightFilterList);
    adapter.notifyDataSetChanged();
}

And the result of filtering*:

enter image description here

    • sorry for wrong toast text. It shows the whole list size.