android - Populating Expandable Listview with SQLite database -
i have sqlite database has columns id, homework, classname, date. have expandable listview. code works , creates expandable listview duplicates group titles.
the group names classname column, , child objects added group same classname. expandable listview takes each classname when need take each unique classname doesn't duplicate groups. here expandable listview adapter:
public class expandlistadapter extends cursortreeadapter { sqlitehelper values; private layoutinflater minflator; context context; textview groupheader; public expandlistadapter(cursor cursor, context context){ super(cursor, context); this.context = context; values = new sqlitehelper(context); minflator = layoutinflater.from(context); } @override public view newgroupview(context context, cursor cursor, boolean isexpanded, viewgroup parent){ final view view = minflator.inflate(r.layout.group_header, parent, false); return view; } public void bindgroupview(view view, context context, cursor cursor, boolean isexpanded){ groupheader = (textview) view.findviewbyid(r.id.group_name); if(groupheader != null){ groupheader.settext(cursor.getstring(cursor.getcolumnindex("class"))); } } @override public view newchildview(context context, cursor cursor, boolean islastchild, viewgroup parent){ final view view = minflator.inflate(r.layout.row_expandable, parent, false); return view; } @override public void bindchildview(view view, final context context, final cursor cursor, boolean islastchild){ textview homework = (textview) view.findviewbyid(r.id.homeworkdisplay1); if(homework != null){ homework.settext(cursor.getstring(cursor.getcolumnindex(values.key_homework))); } textview date = (textview) view.findviewbyid(r.id.duedisplay1); if(date != null){ date.settext(cursor.getstring(cursor.getcolumnindex(values.key_date))); } textview classes = (textview) view.findviewbyid(r.id.classdisplay1); if(classes != null){ classes.settext(cursor.getstring(cursor.getcolumnindex(values.key_class))); } } @override protected cursor getchildrencursor(cursor groupcursor){ values.open(); cursor childcursor = values.fetchchildren(groupcursor.getstring(groupcursor.getcolumnindex("class"))); childcursor.movetofirst(); values.close(); return childcursor; } @override public void ongroupcollapsed(int groupposition){ } @override public void ongroupexpanded(int groupposition){ } }
and here sqlitehelper class: public class sqlitehelper{
public static final int database_version = 1; public static final string database_name = "homeworkmanager"; public static final string table_name = "homeworklist"; public static final string key_id = "_id"; public static final string key_homework = "homework"; public static final string key_date = "date"; public static final string key_class = "class"; private sqlitedatabase db; private dblisthelper myhelper; cursor db_cursor; context context; class dblisthelper extends sqliteopenhelper { public dblisthelper(context context) { super(context, database_name, null, database_version); } @override public void oncreate(sqlitedatabase db) { string create_table = "create table " + table_name + " (" + key_id + " integer primary key autoincrement, " + key_homework + " text ," + key_date + " text ," + key_class + " text)"; db.execsql(create_table); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists " + table_name); oncreate(db); } } public sqlitehelper(context context){ this.context = context; } public sqlitehelper open() throws sqlexception{ myhelper = new dblisthelper(context); db = myhelper.getreadabledatabase(); return this; } public void close(){ if(myhelper != null){ myhelper.close(); } } public long insertdata(string homework, string data, string classes){ contentvalues values = new contentvalues(); values.put(key_homework, homework); values.put(key_date, data); values.put(key_class, classes); return db.insert(table_name, null, values); } public cursor fetchgroup(){ string query = "select * homeworklist"; return db.rawquery(query, null); } public cursor fetchchildren(string class_name){ string query = "select * homeworklist class = '" + class_name + "'"; cursor cursor = db.rawquery(query, null); return cursor; } public void remove(long id){ string whereclause = "_id=?"; string[] whereargs = new string[]{string.valueof(id)}; db.delete(table_name, whereclause, whereargs); } }
thanks in advance :)
hi there instead of passing cursor constructor public expandlistadapter(cursor cursor, context context){ why not replace list intended listview
public expandlistadapter(context context, list parent, list child){ wont have fetch data in every view prevent data being duplicated
Comments
Post a Comment