- #1
- 2,139
- 2,715
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:
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
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
I have a database, and the following three tables are of interest here:
itemEntity
with columns:itemID
: integer primary keyitemName
: String; indexed to be unique.
categoryListEntity
, with columns:categoryID
: integer primary keycategoryName
: String; indexed to be unique.
itemCategoryEntity
, with columns (Composite primary key)itemID
(with a Foreign key toitemEntity
)categoryID
(with a Foreign key tocategoryListEntity
)
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
}
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:- 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. - Directly search the database, create a new (or recreate the old)
itemDataList
, and pass that to Android.
- 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.
- 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.