javaandroidsqlitepaginationgreendao

How do I use GreenDao LazyList correct for huge amount of data + Sort and Filtering


I'm trying to implement paging in android listview.

Background: I download around ~60k datasets via webservice and save them into a SQLite db using GreenDao. Now i want to access this 60k rows in a ListView and select some (0-10). The selected items are send to another ListView containing only the selection. The db entries are ~3 MB for all 60k entries.

Whats already implemented:

The ListView is shown in a DialogFragment. I came up with the idea of using the GreenDao LazyList class, because this list can loaded single items without directly loading all items.

Because LazyList doesn't allow changes to the list (remove, add, clear)

I did the following: The ListView has an Adapter class that uses a java.util.List in the DialogFragment I query for the LazyList (get all) or use an EditText to apply a like clause.

The first time I query the 60k entries I load the first 50 items into the adapter and display them in the ListView. I wan't to use a navigation bar to change the items later.

I want the EditText to work directly while the user is typing so i added a TextChangeListener.

edittext.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if(lazyList != null) {
                if(!lazyList.isClosed()) {
                    lazyList.close();
                    lazyList = null;
                }
            }

            if(s.length() <= 1) {
                QueryBuilder qb = session.getCodeSystemDao().queryBuilder();
                lazyList = qb.listLazyUncached();
                resetListView();
            }
            else {
                QueryBuilder qb = session.getCodeSystemDao().queryBuilder();
                qb.where(DataDao.Properties.Value.like("%"+s.toString()+"%"));
                lazyList = qb.listLazyUncached();
                resetListView();
            }
        }
    });

The problem is: while only typing 1-4 characters a list won't show because the cursor memory is full see Log:

W/CursorWindow: Window is full: requested allocation 48 bytes, free space 19 bytes, window size 2097152 bytes

I could use the GC after closing the list but using GC directly in java is a pain. Is there a better way to do this? (actually I tried that once and didn't work).

I need the the values to be searchable (way to many entries) and it must be sortable.

If I click on a button "sort alphabetically" it does use both (edittext [for like] and ORDER) in a GreenDAO QueryBuilder to load a new lazylist (like the textwatcher + order clause).


Solution

  • You probably should not use LazyList here. Paging is usually done with a query using LIMIT and OFFSET parameters.

    Check the "Limit, Offset, and Pagination" section here: http://greenrobot.org/greendao/documentation/queries/