Set image dynamically in a listview by retrieving image name from a data source in android -
what best way retrieve images drawable
folder while name of images stored in db
, show them in listview
?
suppose have 3 pictures in drawable
folder , names stored in db as:
pic1.jpeg
image2.jpeg
- another_image.jpeg
i have method called getallimages()
retrieve image names , return them cursor
db:
public cursor getallimages(){ string sql = "select iid _id," + column_img_desc + "," + column_img_name + " " + table_name"; cursor cursor = db.rawquery(sql, null); if(cursor != null) cursor.movetofirst(); return cursor; }
where column_img_desc
image description
, column_img_name
name of image
stored in db
then have cursoradapter
tried map images , descriptions listview
:
listview customlistview = (listview)findviewbyid(r.id.lvcustom); string[] = { databasehelper.column_img_desc, databasehelper.column_img_name}; int[] = {r.id.ivimg, r.id.tvtitle}; simplecursoradapter cursoradapter = new simplecursoradapter(this, r.layout.custom_listview, cursor, from, to, 0); cursoradapter.setviewbinder(new simplecursoradapter.viewbinder() { @override public boolean setviewvalue(view view, cursor cursor, int columnindex) { imageview imageimageview = (imageview)findviewbyid(r.id.ivimg); string imagevalue = cursor.getstring(1); //get image names int[] imgresourceids = new int[cursor.getcolumncount()]; //initialize array resource ids string images[] = new string[cursor.getcolumncount()]; //initialize array image names for(int i=0; i<cursor.getcolumncount(); i++){ images[i] = imagevalue; //store image names initialized array imgresourceids[i] = getresources().getidentifier(images[i], "drawable", packagename); //get image name imageimageview.setimageresource(imgresourceids[i]); //set image imageview } return true; } }); customlistview.setadapter(cursoradapter);
but got result: how tan solve this?
thanks in advance :)
you can image drawable folder dynamic name way:
resources.getidentifier(string imagename, string folder, string package);
this returns integer same way type
r.drawable.imagename;
an example of how use it:
bitmap bitmap[] = new bitmap[5]; (int = 0; < 5; i++) bitmap[i] = bitmapfactory.decoderesource( resources, resources.getidentifier("pic" +i, "drawable", "com.example.com")));
this gets images "pic0.png", "pic1.png" ... until "pic4.png", of course can customize way want
Comments
Post a Comment