Shanaz K
Shanaz K

Reputation: 698

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

Upvotes: 0

Views: 2853

Answers (2)

Juraganerka
Juraganerka

Reputation: 35

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/

Upvotes: 2

Dr NotSoKind
Dr NotSoKind

Reputation: 225

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.

Upvotes: 0

Related Questions