Java – How can I properly handle large amounts of data + sort and filter using GreenDao LazyList

How can I properly handle large amounts of data + sort and filter using GreenDao LazyList… here is a solution to the problem.

How can I properly handle large amounts of data + sort and filter using GreenDao LazyList

I’m trying to implement paging in android listview.

Background: I downloaded about 60,000 datasets through a web service and saved them to a SQLite database using GreenDao. Now I want to access those 60k rows in the ListView and select some (0-10). The selected item is sent to another ListView that contains only the selection. Database entries for all 60k entries are approximately 3 MB.

What has already been implemented:

  • SQLite database
  • DAO
  • ListView
  • Adapter
  • Transferlogic Data ListView -> The selected data ListView

The ListView appears in the DialogFragment. I thought of using the GreenDao LazyList class because this list can load a single item without having to load all items directly.

Because LazyList does not allow changes to the list (delete, add, clear).

I did the following:
The ListView has an Adapter class that uses java.util.List in the DialogFragment to apply the like clause using java.util.List.

The first time I query for these 60k entries, I load the first 50 entries into the adapter and display them in the ListView. I don’t want to use the navigation bar to change items later.

I

wanted EditText to work directly on user input, 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: when only 1-4 characters are entered, the list does not appear 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 can use GC after closing the list, but using GC directly in java is a pain. Is there a better way? (Actually I tried once without success).

I need a searchable value (the way many entries) and it has to be sortable.

If I click the “Alphabetical” button, it loads a new lazy list (like like and ORDER) using both (edittext [for like] and ORDER) in GreenDAO QueryBuilder.

Solution

You probably shouldn’t be using LazyList here. Pagination is typically done by using queries with LIMIT and OFFSET parameters.

Check the “Limits, Offsets, and Pagination” section here: http://greenrobot.org/greendao/documentation/queries/

Related Problems and Solutions