Searching & Displaying Items in Android App Using Java & SQL

In summary, the best approach for searching through a large database in an Android app will depend on the specific context and implementation. Generally, using an appropriately indexed database is faster and more efficient than looping through a Java array. However, there may be some cases where looping through a list is more efficient, such as when searching for specific characters. The best approach may also involve a combination of both methods, depending on the specific requirements and limitations of the app.
  • #1
Wrichik Basu
Science Advisor
Insights Author
Gold Member
2,116
2,691
This is actually for my new Android app. But everything is written in Java and SQL; there is almost nothing that is specific to Android.

I have a database, and the following three tables are of interest here:
  • itemEntity with columns:
    • itemID: integer primary key
    • itemName: String; indexed to be unique.
  • categoryListEntity, with columns:
    • categoryID: integer primary key
    • categoryName: String; indexed to be unique.
  • itemCategoryEntity, with columns (Composite primary key)
    • itemID (with a Foreign key to itemEntity)
    • categoryID (with a Foreign key to categoryListEntity)
I have to display the item name and its categories (along with some other details of each item) to the user. I have created a class that holds the necessary data for each item. The class looks somewhat like this:
Java:
public class ItemData {

    private final String itemName;
    private final ArrayList<String> categories;
    private final ArrayList<Long> packSizes, quantityStock, quantityInUse;

    public ItemData(@NonNull String itemName, @Nullable ArrayList<String> categories,
                                     @Nullable ArrayList<Long> packSizes, @Nullable ArrayList<Long> quantityStock,
                                     @Nullable ArrayList<Long> quantityInUse) {
        this.itemName = itemName;
        this.categories = categories;
        this.packSizes = packSizes;
        this.quantityStock = quantityStock;
        this.quantityInUse = quantityInUse;
    }
   
    // Getters omitted for simplicity
   
}
As you can see, each instance of this class is for one item. I haven't included the itemID and categoryIDs, because they won't be displayed to the user. In addition, one item can have more than one category. The class instances are created by reading data from the database; there is an ArrayList<ItemData> itemDataList that contains all such objects. In Android, this list is used to display the items to the user. Also, assume that this list has a large number of items.

I want to carry out some search queries. For example, I may want to find all the items that have a certain category, or I may want to find all items that start with some specific characters. Note that in order to display the filtered items to the user, I have to pass an itemDataList with only those filtered items. I have two options for doing this:
  1. Loop through the old itemDataList, find the items which have that particular category or start with some specific letters, put them into a new list, and pass that to Android.
  2. Directly search the database, create a new (or recreate the old) itemDataList, and pass that to Android.
I want to show search results as soon as the user is typing. So, with each letter the user types, the results will change.
  1. If I choose option 1, I will have to use loops. Sometimes I will have to use more than one loops (for example, when I want to search the list of categories for each item). This is often considered inefficient.
  2. Database queries are often resource-consuming (and preferably should be done in child threads and not the UI thread), because I will have to read from the internal storage. For small databases, it doesn't matter, but for a sufficiently large database, it will consume a lot of system resources if I continuously query the database for each letter the user types. Plus, I am not retrieving one column; I am retrieving a total of four columns each time I query.
So, which will be faster, and which is efficient? Can speed and efficiency be combined?
 
Technology news on Phys.org
  • #2
I assume you are using Android's builtin SQLlite implementation: this obviously is specific to Android and the answers will differ for other implementations.

Generally speaking, queries that use an appropriately indexed database are faster and consume less resources than userspace code. This will almost certainly be the case for
Wrichik Basu said:
I may want to find all the items that have a certain category
but
Wrichik Basu said:
, or I may want to find all items that start with some specific characters.
introduces issues of UTF-8 character equivalence and other complications so YMMV.

Wrichik Basu said:
  1. Loop through the old itemDataList, find the items which have that particular category or start with some specific letters, put them into a new list, and pass that to Android.
  2. Directly search the database, create a new (or recreate the old) itemDataList, and pass that to Android.
What do you mean 'pass that to Android'? Is this actually a client-server webapp not an Android app? If that is the case then your performance will be entirely dependent on network latency and all other considerations are irrelevant.
 
  • #3
pbuk said:
I assume you are using Android's builtin SQLlite implementation
To be exact, I am using Room to connect to and query the database. Room is a layer of abstraction over SQLite so that the developer does not have to directly deal with Cursor (which is very prone to NullPointerException).
pbuk said:
What do you mean 'pass that to Android'? Is this actually a client-server webapp not an Android app?
No, not client-server app. Actually I will pass that ArrayList to an adapter that extends Android RecyclerView.Adapter class. The adapter is, in turn, responsible for setting the view by reading the ArrayList.
 
  • #4
OK, so in that case the answer is 'it depends'. It is obiously going to be quicker to eliminate one entry from a list of 1,000,000 entries in a Java array than to query the 999,999 remaining ones anew, conversely it is going to be quicker to retrieve 1 entry from an abstraction over a well indexed SQLlite table than to sequentially search for 1 entry in a 1,000,000 entry Java array. Somewhere in the middle there will be no difference, and this will vary depending on the hardware and what else is running on it at the time.
 
  • Like
  • Informative
Likes Wrichik Basu and Vanadium 50
  • #5
IME this question does not arise in practice because you don't cache the full query results in an array, you are working with a LIMITed set with something like the following pseudocode:
Code:
inputHandler(event)
  // Don't query until we have meaningful input.
  if event.data.length < 3 return
  query = "SELECT id, name FROM categories WHERE name SOUNDS LIKE ? LIMIT 5"
  try
    // Query the DB asynchronously.
    result = wait for
      execute query with event.data
    // Display up to 5 query results to the user.
  catch err
    // Handle exception.

// Let the user finish typing before firing the handler.
bind debounce(inputHandler, ONE_SECOND) to userInput
 
  • #6
I have done both - even a hybrid one. Here is how the hybrid worked. I initially searched the database and the program was returned by the computer operation staff as running too long. So what I did was keep pointers and put them at the end of an array as I went along. It reduced running time considerably. So my advice is to try searching the database, but if that takes too long use a buffer you search first.

Thanks
Bill
 
  • Like
Likes Wrichik Basu

1. How can I search for a specific item in an Android app using Java and SQL?

To search for a specific item in an Android app using Java and SQL, you can use a SQL query to retrieve data from the database based on specific criteria, such as item name or ID. You can then display the results in your app through a list or a search bar.

2. Can I filter the search results in my Android app?

Yes, you can filter the search results in your Android app by using SQL queries to retrieve data based on specific criteria, such as price range or category. You can also use sorting algorithms to arrange the results in a particular order, such as alphabetical or by popularity.

3. How can I display the search results in a visually appealing way?

To display the search results in a visually appealing way, you can use custom layouts and design elements in your app. This can include using images, icons, and different fonts to make the results more visually appealing and user-friendly.

4. Is there a way to improve the search speed in my Android app?

There are several ways to improve the search speed in your Android app. One way is to optimize your SQL queries by using indexes and avoiding unnecessary joins. You can also implement caching to store frequently searched items and reduce the need to query the database every time. Additionally, you can use multithreading to perform searches in the background while the user continues to use the app.

5. Can I implement a search history feature in my Android app?

Yes, you can implement a search history feature in your Android app by storing the user's previous search queries in a database or shared preferences. You can then display this history in a drop-down list or a separate screen for the user to select from. This can help improve the user experience and make it easier for them to access previous searches.

Similar threads

  • Programming and Computer Science
Replies
3
Views
650
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
7
Views
435
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
638
  • Computing and Technology
Replies
5
Views
2K
  • Programming and Computer Science
Replies
21
Views
2K
Back
Top