Sorry for the silly question but im stucked
I want to send a large amount of data to other Activity through putExtra.
Is there any way to convert the "string" in putExtra("string", data ) to a variable given that it's stupid to write 100 different strings;
The putExtra method stores the data as a key/value pair. The string is like an index to the data it is not really a form of data in itself. When you want to retrieve your data you give the string and the system then finds the string in its indexing system and pulls out your data. Think about a book. If you want to find out about a particular subject (i.e. read some data) it would be time consuming to read the whole book to find out what you need. So instead you just check the index and you are able to go straight to the section you want and get the data you need.
Put simply the answer is NO. You must use a string and that string value must stay constant so that you can retrieve your data.
The string is simply the name that the data will be referred to as. If you want to pass 100s of strings then that's pretty easy, you just have to make data be a structure that contains your strings:
String[] data = new String[200];
... fill in the strings ...
putExtra("multipleStrings", data);
Related
One string size is about 200 bytes,
and it stores 10~20 size in a daily array.
(Store 10~20 strings of 200bytes, as array type)
I have found a way to convert an array to a string
and store it in SQLite.
However, I do not know it's a good idea
because the size of the string is large.
1.
If large arrays of strings,
is it a good idea to store arrays as a string?
2.
or is there a better way?
I would like advice. Thank you.
You're actually placing your concern onto the wrong part of your database design.
For SQLite, the maximum length of a String is 1 billion bytes, so your worries about your 10-20 strings of 200 bytes each actually isn't considered that large.
There's really no harm in storing your array as a single long String in your database. Especially when it's nowhere close to the maximum limit of a String.
Your database query time won't become longer due to your String being long. The real concern here is the processing you'll be doing on that String to turn it back into an Array. Typically, if the String is extremely long, the real performance hit is when you're flattening the array into a String and when you're transforming that String back into an Array.
However, typically, this is something you'll show a loading indicator for to your users.
For storing an Array into a database, there's really only two ways to do so:
Flatten array into a single String and store the String as TEXT
Create a table meant to store the individual elements of the string, and include a column for a Foreign Key that allows you to associate those rows with the same array. Then you'll store each element of your String arrays as a row in this table.
Depending on what you need, one design is better than the other.
For example, you would normally prefer the second implementation if your app requires you to constantly edit individual elements of an array.
For such an example, it wouldn't make much sense to use the first solution, because this means every time you want to edit the contents of an array, you'll be fetching back the complete array in it's entirety. This is impractical when you only want to fetch or edit a particular portion of that String.
Therefore, in such an example, it is much more practical to store the individual elements of the arrays into individual rows of a Table meant for this type of data. You'll be querying only the row you want and updating only the row you want.
So to answer your questions, there's really only two ways to store your String array as a TEXT type in your SQLite database. Both ways work and the real concern is to consider which solution fits your needs best.
If your app only requires you to store and fetch the array in it's entirety each time, then the single String method might be more preferable.
But if your app requires you to work with individual elements of your array, then using the table method would be more convenient.
can i store two or more values with same key using SharedPreferences in android? If no, please tell me how to store values of username, first name, password etc when many users register in registration app?
Ex:
person A registered with username="john12", first name="john" and DOB="06/06/2000".
person B registered with username="arun89", first name="arun" and DOB="08/11/1989".
Now, I want to store these values in SharedPreferences and retrieve them later. Is it possible using SharedPreferences? If not, Please tell me how to do in other way.
Thank you in advance.
I woud consider creating a JSONObject and add the fields you want to store as a key:value pair.
json.putString(key, value);
You can then store the json object in it's string representation with json.toString() and restore it later with
JSONObject jo = new JSONObject(jsonString);
String value = jo.getString(key);
JSONObject also offeres different data types beside strings.
It really depends on how much data you want to store. Depending on that I would choose SharedPreferences or a SQLite implementation.
You cannot store these values directly (as ones added latter will overwrite previously added) but you can always store Parcelable and put your data into it
For your case it is better use SQLIte database.But if you want to use shared preference it is still possible.You have to use a key with additional index to remember different user like
UserName1:arun
UserName2:john
You have to remember the total number of user.Then can maintain all of them.you can also use other data structure like hashmap to maintain data for the shared preference.
I dont't think it is possible, as you don't know the number of users.
You could try to separate the users with commas, but that's lame.
You should consider using SQLite database.
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
Then you have to store a array List of user objects first create a class userInfo then create a array List type of userInfo then store the data in this list and put a serialize-able object in SharedPreferences.
You can also store them on a single key called "registers" as string. Concatenate each register to preference. Put ";" (or any other characther you want) between each register. Then parse the string and use the values.
Key: registers
Value: "username=john12, first name=john, DOB=06/06/2000;username=mike12, first name=mike, DOB=06/07/2012"
Using split method of String will give you a list of registers as String.
registers.split(";");
Splitting again with "," will give you properties of each register.
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>);
I have a String array that I want to keep to display in a ListView, but every time I exit the activity, the global String array is deleted and the ListView doesn't display. Is there anyway that I can keep a String array in the application's storage for the next use?
It's actually pretty easy for just a string. You can store it in shared preferences. See this link: http://developer.android.com/guide/topics/data/data-storage.html#pref
This is the problem:
I have a form for inputting names, then saves it, then displays it in another activity. But once I try to enter another name, the previous name is overwritten by the new name.
Is there a way to set these names up to list themselves one after another without overwriting each other in SharedPreferences?
You can as long as they have distinct names. Ig you need multiple values for same name, you can store JSON array or use some form of prefix / suffix solution to provide unique names
Either do it like Konstantin Pribluda suggested, or you might think of using the SQLite, if you have a lot of names you want to store (e.g. if creating a history of typed in names). That way you can store unlimited values for the same key and retrieve them as a list/cursor.
But of course that's overkill if you only have 2-3 names…
you can also save a Set of Strings in the SharedPreferences.