How to fetch SQLlite data in a shortest time possible - android

I am trying to fetch a data from an Sqlite and bind it to a Recyclerview.The problem I am facing here is it is taking so long to get all the data from the database and add it to an Array List(I am using AsyncTask). I also have tried to fetch only the datas which matches to the text I am looking for,Still it takes long time. I tried to save the ArrayList in a SharedPreferences and use it when needed but I couldn't save ArrayList of a specific class to a shared Preference(it doesnt support). which way can i solve this problem?
I apologize for my English.

Related

How to store a small list, on Android? SharedPreferences VS SQLite VS file

I have an app that generates objects of a class, let's call it X, that is Serializable. I want the user to be able to occasionally save or delete objects of X from his/her list of favorites (a list that can go up to 100 objects).
What is the most appropriate way to persistently store the list of favorites?
In SharedPreferences, storing the whole list as a JSON String
In an SQLite database:-
Storing one item per object, as BLOB's
Storing one item per object, as JSON Strings
In a custom file, storing the whole list as a JSON String
Some other way?
My thoughts are:
Because adding and removing favorites will be occasional, and the list is small, I probably don't need the advantages of a DB when it comes to searching fields in large amounts of data. So I am inclined to maintain a local ArrayList, add and remove items from it, and save it to SharedPreferences (option 1).
It seems odd to save a key-value pair holding an entire list as a JSON String, I'm afraid I might be unaware of some sort of limitation.
Is there a limit to the size of the String I can store in SharedPrefferences?
Is it too problematic that I add or remove objects from my local ArrayList and then save the whole list?
I am agree with your first approach because of It's manage easily and need small storage space.

How to save RecyclerView content

I'm using a RecyclerView as a checklist that can be used as a grocery list for example, first the user adds the product(item), the price and the store, then my app adds this item object into my ArrayList adapter; each item in my adapter is a relative layout with 3 TextViews for the product, price and store. The problem occurs when the user shuts down his device or closes the app: the RecyclerView resets to nothing.
What is the easiest way to save items in a RecyclerView so that it stays? Do I save in the cache?
Shared Preferences
or
SQLite Databases
You will need to save the data to persistent storage in some way. For something small, it would be best to use SharedPreferences. Then, when you re-open the app, load the data from SharedPreferences into your ArrayList, then populate the RecyclerView.
To use SharedPreferences you will need to convert your data into a String, because SharedPreferences can only handle primitive data types. The best way to convert lots of data to a String is by converting the data into a JSONObject, then that JSONObject into a String (which can be stored in SharedPreferences).
JSON is like a dictionary, it's a way to store Key-Value pairs. See this SO question for more information and some examples of what it looks like. Then search for how to use JSON and Android (or reference the docs).
EDIT: If your RecyclerView is showing a simple amount of data, you may be able to store a Set<String> in your SharedPreferences. See this relevant documentation.

How to read all keys and values from a shared preference

I am doing a simple application where a ListView is populated from the data stored in SharedPreferences. I need to read all the key pair values from SharedPreference. I used the code given in another post with same question, but it has not helped me at all.
The code is using a Map to getALL() from SharedPreference. When I try to print the count() of keys in the MAP it is always giving zero count. I am stuck in my application building due to this problem.
Can somebody help me with a simple code to retrieve all the key and values from a SharedPreference? Thanks.
You can store and retrive arraylist or array data like this Example.
You are fetching data into list but you are not storing data into sharedprefence from arraylist.
In this situation(never tried but this may work)
Store
retrive size variable as in above example
increament size by 1(to store one value)
store value in SharedPreference
Store size in SharedPreference.
Fetch
retrive size variable as in above example
then go through loop for all values

how can I put the data retrieved in a string array

I am working on an app, which takes data from the user, stores the data in the database. The data is retrieved using cursor, and should be put in a string array. I am successful till the retrieving part, but how can I put the data in a string array? and is using string array the right approach here?, I mean data is being added continuously and the string array size is fixed. Not sure what to do here, please help.
Okay found a solution here http://www.anddev.org/working%5Fwith%5Fthe%5Fsqlite-database%5F-%5Fcursors-t319.html
, using ArrayList would help here.
#sam You got to use ArrayList<String> for ur requirements.u can add ur retrieved data into ArrayListby this method:
arrayList.add(<ur retrieved string>);

Array list of double in SharedPreference

I want to know whether it is possible to save as an array list of double in SharedPreference. In my application, I want the 'size' to be saved whenever there is new 'size' written from the user. It must be uploaded to an array list of double, without erasing the previous one.
first of all - the answer to your question is simple:
it's not possible.
but the good news are that there are lots of ways saving array of doubles (as you've been suggested in the comments):
save it to a binary file / serialized file in the internal/external storage, to SQLite database, make it the responsibility of the server side (if there is one..)
you can save the size of the array in the shared preferences if it helps you read the data from the file containing the serialized array, but I guess you already know that..
anyway - good luck

Categories

Resources