Android: initializing a list with a large number of items - java

I need to use a list with more than 31 thousand strings for a dropdown menu. Now, if I declare this List/Array in java, I get the error that says the code is too large to compile. On the other hand, if I put these values in string-array in strings.xml, I get the message Resource compilation failed when I try to build the application.
What else can be done to store this list?
I don't want to keep the data in server and fetch it using API because it will increase my server cost.

You could put the data into a table in a local SQLite Database (no servers needed), and then retrieve only the visible part when needed. I suggest to use a RecyclerView to show the list of item: it needs a bit more time to be implemented, but works very well with big lists of data.
Assuming that the list is static, you can include a pre-filled Database in your assets folder and copy it into the /databases folder when your application first launches. No need to fill it by the app using big arrays.

Related

Can i replace my table-arranged dummy values with actual data froma database?

[So as you can see in the picture, i'm a newbie in android programming and i just had an assignment concerning creating a management system app for university.
The views you see in the picture are just customly made with xml(Table layout) and the data are just dummy data placed on the respective textviews in the tables. I desire to create a database for students and all those data e.g names, gender, should be fetched from the database or sent. Remember, as for courses the number of courses would differ from one degree prog to another(imagine a student with less ormore courses than the tables i sampled there).
How do i acomplish this
As for database, im thinking of using SQLite or Firebase ]1
There are many ways to approach this, depending on the requirements of your program. If you need to share data between multiple people or devices, you probably want a database that lives on a web server, and you would write an API (application programming interface) which you would call from your app, and it would go and get the data from the database and return it to your app.
If your app is meant to be 'stand alone', where you could use it without being connected to the internet, then yes, a SQLite database is a good solution and it is pretty easy to integrate into your app.
To create a SQLite database, you can download the free tool SQLiteStudio. Once you create the database (usually saved with a .db file extension) you can add it to your project in the Assets folder (create it if it doesn't exist). The folder lives at the same level as the bin and gen folders in your project.
Next, search for a class called DatabaseHelper.java, it has a number of methods for opening, closing and querying a SQLite database, copy that to your project.
At the start of your program, you should check to see if you have a copy of the database in your local data/databases folder in your App's file storage area. If you don't have a copy there, which on the first execution of your program, it will not be there, then you have to copy the database .db file from your Assets folder to the data/databases folder, then open it there. If it is found, you've already done this, so, just open it.
When you want to read data into your app, you execute a SELECT statement to retrieve data from your database into a Cursor object, and return that cursor from your DatabaseHelper class to your activity. In your activity, you will iterate through the Cursor, reading one row at a time, and copy the data into program variables. Sometimes, you will create a class object to hold one record, with attributes that match a row from your query, and you'll create an instance of your class, fill it with the data from the Cursor, then add that object to an Array List of objects which your program can use at a later time, say, to display a list of people or whatever it is you queried for. Once the Cursor has been read to the end, you close the Database by calling a close method in the DatabaseHelper class.
That's the general idea. If you search for a copy of the DatabaseHelper.java class, that will get you started. Then, search for some example projects that use that DatabaseHelper.java class so you can figure out how to use it.
Good luck!!

Problem on using indexOn on deeper nested item

I have search a lot on stackoverflow and read many questions
I was having 3 indexOn problem 2 of them are solved and 1 remains
I am sorting database and have indexOn on "favorite" and "poet" which runs successfully but I need one more indexOn for numbers inside heart node.
query is running successfully but I am getting indexOn warning in android studio
I have tried using variables in place of numbers in database rule but still getting warning
Using an unspecified index. Your data will be downloaded and filtered on the client. Consider adding '".indexOn": "heart/+91916*******"' at gazal to your security and Firebase Database rules for better performance
queryFav = FirebaseDatabase.getInstance()
.getReference(reference).orderByChild(child).equalTo("heart");
above query run successfully but what should be indexOn rule
The message you get means you are running a query that has orderBy("heart/+91916*******") on a node named gazal. To efficiently run that query, you need to have an index on heart/+91916******* to that node in your security rules. But since the +91916******* part of that index probably is dynamic (i.e. you'll have a different value of +91916******* for every user of the app), you'll have to add an index for each user. That is not feasible.
In other words: you current data structure makes it easy to read the users who have hearted a specific poem. It does however not make it easy to determine the poems that a specific user has hearted. To make that equally easy, you'll want to add an additional data structure"
"user_hearts": {
"+91916*******": {
"-KjYiXzl1ancR8Pi3MfQ": true
}
}
With the above structure you can easily read the user_hearts node for the user you're interested in, and then read the poems one by one if needed.
Also see:
Firebase query if child of child contains a value
Firebase Realtime Database - index on location containing uid

Performance of database call from JAVA to Database

Our team is building a small application wherein a UI has about 10 drop-down list boxes. ( DDLB ).
These list boxes will be populated by selecting data from different tables.
Our JAVA person feels that making separate database call for each list will be very expensive and wants to make a single database call for all lists.
I feel it is impractical to populate all lists in one database call due to following reason
a. Imagine an end user chooses state = 'NY' from one DDLB.
b. The next drop down should be populated with values from ZIP_CODES table for STATE='NY'
Unless we know ahead of time what state a user will be choosing - our only choice is to populate a java structure with all values from ZIP_CODES table. And after the user has selected the state - parse this structure for NY zipcodes.
And imagine doing this for all the DDLB in the form. This will not only be practical but also resource intensive.
Any thoughts ?
If there are not many items in those lists and memory amount allows you could load all values for all drop boxes into memory at application startup and then filter data in memory. It will be better then execute SQL query for every action user makes with those drop boxes.
You could also use some cache engines (like EhCache) that could offload data to disk and store only some fraction in memory.
You can run some timings to see, but I suspect you're sweating something that might take 100th of a second to execute. UI design wise I never put zip codes in selection menus because the list is too long and people already know it well enough to just punch in. When they leave the zip code field I will query the city and state and pre-fill those fields if they're not already set.

What is the fast way to sort

Problem Description
I'm writing Android application which is working with big data, I have database (15 mb) and my application shows data from it. I have a queries which are get data from database already sorted for example alphabetic or depending on some parameters which I have provided.
Question
As I store data in the Array and then show it to user I want to know what is the fast way to sort data, while making a query or just put data in array and then sort it?
i am also faced this situation in my application. i resolved performance by using following way.
First i created index on my table based on primary key.
Then i used order by to sort the elements.
To search it in local i kept total content in one object , then perform search on that object.
If you use these surly you will improve performance 200 %.

Optimised update ListView in android

Till now, I have coded apps which load listviews completely from web(parsing,etc) or completely from local database. What I intend to develop is a listview which will load from existing local database, and check the web database and fetch only those entries which do not exist in the local cache, and then update the local cache with those new entries.
I had a naive idea of implementing it. There would be a single value in local data about the number of entries in local database. Similarly, there would be a value of number of entries on the web database. Then we can exactly fetch the required number of entries from the web, instead of fetching the entire list again.
Is there some better/easier way to do it?
I found a solution to this. The better thing to do would be to do this checking on a middle layer server. It would be like a 3 layer application. There would be a middle layer which would store all the posts seen by the current user. So next time user wants to update himself, the middle layer can check what results were posted back to him last time

Categories