0

I am creating a clothes shopping cart app.

My existing SQLite database has all the items and their information and I need to display each item in a listView. I need to display the item's information such as the brand, price, size, store, etc. and also an image of the item.

So the images of the items aren't in my database though, but I do have them saved in a folder on my computer and I do know which items they go with and also each item in the database does have an id with it.

So in order to show the image with each item and show the correct image in the listView, can I just put all the images into my drawable folder and then match them up on the ID with each item I retrieve from my database?

I already have my listView set up, it's displaying everything except the images for each item, so does anyone know how I can do this?

Tanmay Patil
  • 6,882
  • 2
  • 25
  • 45
Robert
  • 55
  • 8
  • Why don't you simply put those images to database? – Yuva Raj Apr 17 '15 at 16:46
  • Yes, I did think of doing that, but I have no idea how to put images into a database, do you know how we can do that and then how to get them out of the database and put them into my listView? – Robert Apr 17 '15 at 16:49
  • http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/ – Yuva Raj Apr 17 '15 at 16:50

1 Answers1

0

First of all where do these images come from ? Although technically you can save images in database as BLOB (Binary Large Object) but you cannot seed the database on pre-install. If the images are static then you need to put them as resource. If the images are dynamic (as in retrieved through API call) then a better way than storing them as BLOB is simply save them as-is in SD card or internal storage with names that correspond to id.

This is what I would do. I would add a "imgname" column in the db table . This column is filled with file name of the corresponding image in drawable. It will be returned along with other item information on a query. Now the question is how can we find the resource integer given a file name (Since all views including ImageView don't take file name) ? Use this:

int xlShirtId = MyActivity.this.getResources().getIdentifier("shirt_xl", "drawable", 
  MyActivity.this.getPackageName()); // where shirt_xl is the file name

Then simply set any ImageView in any Activity that needs to display that shirt with imageView.setImageResource(xlShirtId);. Of course you don't have to create this new column if you are fine with id. Just name all the files with id. However id might not be very readable.

In any case remember that you need to preload the database.

inmyth
  • 8,880
  • 4
  • 47
  • 52
  • I just got the images from online and now I have them saved on my computer in a file. So would the best thing to do be just store all of them in my drawable folder or if I can put them into my database, how can I insert images into my already existing database and then retrieve them for the listView? – Robert Apr 17 '15 at 16:59
  • Putting them in drawable would be the best. As I mentioned, images are saved as BLOB in sqlite and this BLOB is basically a byte array. Therefore retrieving it from database will not give you an image. You will still need to reconstruct it as Bitmap, which means extra work. – inmyth Apr 17 '15 at 17:30
  • Yeah I think then I'll just put them in the drawable folder, so then when I populate the listView, I understand that I would just need to match them up on the id in the database right? Also I would like to be able to add an item to a shopping cart which will be another activity so how can I make sure that when I display just those items to the cart, that the images will be there too? Is it just the same thing, use the id to match them up? – Robert Apr 17 '15 at 18:35
  • I see what you're saying with the example that you just gave. There's one problem though, so I am not actually not doing this in my MainActivity, but in my ListItemAdapter, which is my custom adapter for displaying these items. So this is the line that I put: int xlShirtId = ListItemAdapter.this.getIdentifier("shirt_xl", "drawable", ListItemAdapter.this.getPackageName()); But it gives me an error for the getIdentifier and getPackageName methods. – Robert Apr 18 '15 at 03:29
  • You just need to pass Context to ListItemAdapter. Define a constructor like the last answer of this post http://stackoverflow.com/questions/16594557/how-to-get-context-for-baseadapter-in-android . And then in your Activity instantiate it like this: new ListItemAdapter(MyActivity.this) . Then you will be able to use it inside ListItemAdapter like : context.getIdentifier("shirt_xl", ...) – inmyth Apr 18 '15 at 10:59
  • When you say 'saved on my computer', are you talking about an Android device (and thus Android App?) I am assuming YES, just making sure. If yes, what is you IDE? – seanpj Apr 22 '15 at 03:59