Android Tutorial Large & Searchable List with SearchView + B4XSerializator

Better to use: https://www.b4x.com/android/forum/threads/b4x-sqlsearchview-sqlite-based-search-view.133379/

Two very important methods were added to B4XSerializator in RandomAccessFile v2.20:
ConvertObjectToBytesAsync and ConvertBytesToObjectAsync.

With these methods you can asynchronously convert objects to bytes and vice versa. This allows doing things that previously were not possible.
RDC2 for example is based on these methods: https://www.b4x.com/android/forum/threads/61801/#content

SearchView was introduced in 2012. SearchView shows a list with an EditText that acts as a filter.
It uses an in-memory index to find the matching items. SearchView is very useful, however it was limited to about 500 items as it takes time to build the index and the UI is frozen until the index is ready (the main thread is busy building the index).

Now with B4XSerializator and a small B4J program we can easily load 10,000 items or more to SearchView.
The B4J program will create the index and save it into two files. The first file holds the items list and the prefix based index
The second file holds the secondary index (full text index) which is much larger.

In the B4A program we will first load the smaller index and then the larger index (they were added to the Files tab). Both are loaded asynchronously so the UI is not affected.
Until the second index is loaded the user will only be able to search for items based on the prefix. It is highly unlikely that the user will notice this at all.

upload_2015-12-31_15-53-41.png



Why are the new async methods so important here?

On a Nexus 5 it takes 1.5 seconds until the first index is ready and another 5-6 seconds for the second index.
As everything is done in the background, the UI is fully responsive while the data is loaded.
If we were using the non-async methods then the UI would have been frozen for 7.5+ seconds. This is a very bad UI and is also likely to be killed by the OS.

Notes

- This solution is also compatible with B4i as B4XSerializator is cross platform (with some changes to work with iTableView).
- The SearchView class is a modified version.
- The data in this example is Alexa's top 10,000 sites.


I'm sure that going forward we will see more and more usages for B4XSerializator.
 

Attachments

  • B4J_IndexBuilder.zip
    57.9 KB · Views: 1,701
  • LargeSearchView.zip
    733.6 KB · Views: 2,812
Last edited:

Roberto P.

Well-Known Member
Licensed User
Longtime User
1 -When you select an item also returns a number and goes a mistake.
2- I would like to implement the asynchronous mode to avoid slowdowns when data is loaded

Then I wanted to ask if it is correct to use the object in this way or is it better to use it only in combination with B4XSerializator.

Thanks so much
 

Roberto P.

Well-Known Member
Licensed User
Longtime User
I can not use the B4J tool at runtime! I want to avoid creating a file for each object SearchView. I have several in my application.
 

Anser

Well-Known Member
Licensed User
Longtime User
I am also looking for a SearchView solution which can retrieve data from a remote Database via jRDC, display the contents and return the ID of the selected item. For eg Product Name will be displayed and Product_ID of the selected item should be returned.

Because of this limitation, now I am using a Spinner instead of a SearchView.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I can not use the B4J tool at runtime! I want to avoid creating a file for each object SearchView. I have several in my application.
You need to understand how this solution works. The whole idea of this example is to demonstrate how you can prebuild the index to avoid doing it at runtime. Use the regular SearchView instead.

I am also looking for a SearchView solution which can retrieve data from a remote Database via jRDC
There is nothing special about this. You can get the items from wherever you like. Please start a new thread for this.
 
Top