android - Populate ListView with SQLite, Images and Text from local resources -
i have listview able show 2 textviews , imageview per listviewitem. have sqlite database has following fields, _id
, name
, description
, image
.
an example record follows: 1
,r.string.name1
,r.string.description1
,r.drawable.image1
the strings name , description in strings.xml
file, , images needed in res/drawable
folders, referenced r.drawable.image_name
i using sqliteassethelper library manage database.
i able cursor containing information needed, , able populate listview text, when run app, textviews show r.string.name1
, r.string.description1
, etc. have not yet been able image work @ all.
how text show (so can have different languages in future) , how images show?
here code far:
database helper
public class database extends sqliteassethelper { private static final string database_name = "database.sqlite"; private static final int database_version = 1; public database(context context) { super(context, database_name, null, database_version); setforcedupgrade(); } public cursor getlist() { sqlitedatabase db = getreadabledatabase(); sqlitequerybuilder qb = new sqlitequerybuilder(); string [] sqlselect = {"_id","name","description","image"}; string sqltables = "tbl_list"; qb.settables(sqltables); cursor c = qb.query(db, sqlselect, null, null, null, null, null); c.movetofirst(); return c; } }
main activity
public class sqlite_list extends actionbaractivity { private listview listview1; private cursor list; private database db; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.listview); getsupportactionbar().settitle("list"); getsupportactionbar().setdisplayhomeasupenabled(true); db = new database(this); list = db.getlist(); //move own thread later on listadapter adapter = new simplecursoradapter(this, android.r.layout.simple_list_item_2, mods, new string[] {"name","description"}, //table values new int[] {android.r.id.text1,android.r.id.text2}); listview1 = (listview) findviewbyid(r.id.listview); listview1.setadapter(adapter); } ... }
thanks
edit: have written new adapter, images still not working:
public class sqlite_adapter extends resourcecursoradapter { public sqlite_mods_adapter(context context, int layout, cursor c, int flags) { super(context, layout, c, flags); } @override public void bindview(view view, context context, cursor cursor) { textview name = (textview) view.findviewbyid(r.id.txttitle); name.settext(cursor.getstring(cursor.getcolumnindex("name"))); textview description = (textview) view.findviewbyid(r.id.txtdescription); description.settext(cursor.getstring(cursor.getcolumnindex("description"))); imageview image = (imageview) view.findviewbyid(r.id.imgicon); image.setimageresource(context.getresources().getidentifier("image","drawable","com.package")); } }
edit 2: found solution. answer posted below.
ok, got work, instead of using simplecursoradapter, use custom adapter. code can found below.
then use getidentifier
method turn name of image or string, integer can used settext
on textview or setimageresource
on imageview:
public class sqlite_listview_adapter extends resourcecursoradapter { public sqlite_listview_adapter(context context, int layout, cursor c, int flags) { super(context, layout, c, flags); } @override public void bindview(view view, context context, cursor cursor) { textview name = (textview) view.findviewbyid(r.id.txttitle); string name_string=cursor.getstring(cursor.getcolumnindex("name")); int residname = context.getresources().getidentifier(name_string, "string", context.getpackagename()); name.settext(residname); textview description = (textview) view.findviewbyid(r.id.txtdescription); string description_string = cursor.getstring(cursor.getcolumnindex("description")); int residdescription = context.getresources().getidentifier(description_string, "string", context.getpackagename()); description.settext(residdescription); imageview image = (imageview) view.findviewbyid(r.id.imgicon); string image_string = cursor.getstring(cursor.getcolumnindex("image")); int resid=context.getresources().getidentifier(image_string, "drawable", context.getpackagename()); image.setimageresource(resid); } }
so if had string, r.string.name1
, wanted show in textview, in name field of record in database, need name1
similar idea drawables , whatever else might need
Comments
Post a Comment