how can I put the data retrieved in a string array - android

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>);

Related

How to fetch SQLlite data in a shortest time possible

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.

Comparing two ArrayList of different types

In my app, the user can update some data. For example, the images associated. So, I made a db query to know the paths of the images that are already in the Database. I stored the results in a String ArrayList.
I have another custom type ArrayList, BeanFotos. This type has a String which is the path to the images, including the ones that are in the database AND the images which the user just took with the camera.
So, I need to store the images that are not in database, i mean, the one that the user just took.
I am trying this way:
for(int i=0;i<listaFotos.size();i++){//listaFotos is the BeanFotos ArrayList
String ruta=listaFotos.get(i).getFotoPath();
for(int j=0;j<fotosBBDD.size();j++){//fotosBBDD is the String ArrayList
if(ruta.equals(fotosBBDD.get(j))){
listaFotos.remove(i);
}
}
}
storeInDb(listaFotos);
This way, I get unexpected result: One image that already is in the Database, is stored again. How could I do it properly?
Thank you.
To avoid the Redundancy of data use HASHMAP http://www.tutorialspoint.com/java/java_hashmap_class.htm instead of ArrayList.

putExtra sting convert to variable

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);

how to put image json data in listview

how to retrieve all images in MySQL to android
really I can retrieve data like (names,ages,....)
but really I can't retrieve all images
whereas I have a table in MySQL contain
(name , age,Image)
thanks for any guide and any help
i'm not sure this will helpful or not but you cant try this. http://www.androidbegin.com/tutorial/android-json-parse-images-and-texts-tutorial/
Probably you should consider to store the images in the local storage (instead of in a binary format, which is how it seems you are doing it, from your question), and in your database just store the file location. This way you shouldn't have problems converting the data from and to the database, and the database size wouldn't grow too much.
Then in your list view's adapter, you'll only need to retrieve the image's location and load it into the view you want to display.
EDIT
As I understood, you are sending the data to the Android device via Json. So let's assume you are sending a Json array like this one:
[{"username":"John", "userimage":"http://example.com/images/imagejohn.jpg"},
{"username":"Michael", "userimage":"http://example.com/images/imagemike.jpg"},
{"username":"Sophia", "userimage":"http://example.com/images/imagesophia.jpg"}]
So what you need is to read the JSON contents to an Array and fill it in an adapter. As I mentioned in the comments, you should take a look to the answer here: https://stackoverflow.com/a/8113337/1991053
Basically, you can read the JSON Array and put it into the ListView's Array Adapter, in the example above, you can retrieve each element like this:
JSONArray jsonArray = new JSONArray(input) // input is the JSON string you recieve
for(int i = 0; i < jsonArray.length(); i++) {
String userName = jsonArray.optJSONObject(i).getString("username");
String imageURL = jsonArray.optJSONObject(i).getString("userimage");
// Here you can do whatever you want with the values
// like loading an imageview with the url's data
// or putting the values in an ArrayList for filling up the ListView adapter
}
Of course you can modifiy this to fit in whatever the JSON structure you are using for sending the data, just use JSONObjects and JSONArray for this. Take a look to the documentation here: JSONArray, JSONObject
If you want more control over the JSON data (like serialize or deserialize the data), take a look to Jackson or Gson. There are plenty of examples around to understand how they work.
But I think your best bet is to use the adapter shown Here. Also, for loading up images in a ListView from a URL, you can use this library: Lazy List.
Hope this helps you for a start. Best regards.

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

Categories

Resources