i have listview on mainactivity. on listview must image , text. write query , texts displaying. make listcategoryadapter extends baseadapter. tutorials in internet arrays, none of them working db. here adapter.
public class listcategoryadapter extends baseadapter{ layoutinflater inflater; list<category> cats; context context; dbhelper dbhelper; string stt; recordholder holder = null; public listcategoryadapter(context context1, list<category> list){ if (list != null){ this.cats = list; this.context = context1; inflater = (layoutinflater)this.context.getsystemservice(context.layout_inflater_service); } } @override public int getcount() { return cats.size(); } @override public object getitem(int position) { return cats.get(position); } @override public long getitemid(int position) { return position; } private class viewholder { imageview imageview2; textview txtcateg; } @override public view getview(int position, view convertview, viewgroup parent) { if (convertview == null){ convertview = inflater.inflate(r.layout.list_cat_adapter, null); } holder = new recordholder(); final category cat = cats.get(position); holder.txtcateg = (textview)convertview.findviewbyid(r.id.txtcateg); holder.txtcateg.settext(cat.gettitle()); holder.imageview2 = (imageview) convertview.findviewbyid(r.id.imageview2); holder.imageview2.setimageresource(cat.getimage()); return convertview; } static class recordholder{ textview txtcateg; imageview imageview2; } }
here database calls text , image
public list<category> categories(){ sqlitedatabase db = this.getreadabledatabase(); list<category> categorylist = new arraylist<category>(); string ss = "select * category"; cursor cursor = db.rawquery(ss, null); if (cursor.movetofirst()) { { resources resources = mcontext.getresources(); category category = new category(); category.setid(cursor.getint(0)); category.settitle(cursor.getstring(1)); category.setimage(cursor.getint(2)); categorylist.add(category); } while (cursor.movetonext()); } db.close(); return categorylist; }
here category class
public class category { int id; int image; string title; public category(int image, string title, int id) { super(); this.id = id; this.image = image; this.title = title; } public category() { } public int getid() { return id; } public void setid(int id) { this.id = id; } public int getimage() { return image; } public void setimage(int image) { this.image = image; } public string gettitle() { return title; } public void settitle(string title) { this.title = title; } }
Comments
Post a Comment