java - Android: How can I delete an item from a listview that is saved in a SQLite database? -
i've delete item listview saved in sqlite database. can using search "edittext", need remove rows clicking on them. here's code:
dbhelper:
public class eventdbhelper extends sqliteopenhelper { private static final string database_name = "userinfo.db"; private static final int database_version = 1; private static final string create_query = "create table "+ event.neweventinfo.table_name+"("+ event.neweventinfo.name+" text,"+ event.neweventinfo.year+" text,"+ event.neweventinfo.month+" text,"+ event.neweventinfo.day+" text,"+ event.neweventinfo.hour+" text,"+ event.neweventinfo.minute+" text);"; public eventdbhelper(context context){ super(context,database_name,null,database_version); log.e("database operations","database created/opened..."); } @override public void oncreate(sqlitedatabase db) { db.execsql(create_query); log.e("database operations","table created..."); } public void addinformation (string name,string year,string month,string day,string hour,string minute,sqlitedatabase db) { contentvalues contentvalues=new contentvalues(); contentvalues.put(event.neweventinfo.name,name); contentvalues.put(event.neweventinfo.year,year); contentvalues.put(event.neweventinfo.month,month); contentvalues.put(event.neweventinfo.day,day); contentvalues.put(event.neweventinfo.hour,hour); contentvalues.put(event.neweventinfo.minute,minute); db.insert(event.neweventinfo.table_name,null,contentvalues); log.e("database operations","one row inserted..."); } public cursor getinformation (sqlitedatabase db) { cursor cursor; string[] projections = {event.neweventinfo.name, event.neweventinfo.year, event.neweventinfo.month, event.neweventinfo.day, event.neweventinfo.hour, event.neweventinfo.minute}; cursor = db.query(event.neweventinfo.table_name,projections,null,null,null,null,null); return cursor; } public void deleteinformation(string name, sqlitedatabase db) { string selection = event.neweventinfo.name+" ?"; string[] selection_args = {name}; db.delete(event.neweventinfo.table_name,selection,selection_args); } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { } }
mainactivity:
public void deleteevent (view view) { search_name=et.gettext().tostring(); eventdbhelper= new eventdbhelper(getapplicationcontext()); sqlitedatabase= eventdbhelper.getreadabledatabase(); eventdbhelper.deleteinformation(search_name, sqlitedatabase); toast.maketext(getapplicationcontext(),"event deleted",toast.length_short).show(); finish(); startactivity(getintent()); }
listdataadapteractivity:
public class listdataadapter extends arrayadapter { list list = new arraylist(); public listdataadapter(context context, int resource) { super(context, resource); } static class layouthandler { textview name,days,hours,minutes,seconds; } @override public void add(object object) { super.add(object); list.add(object); } @override public int getcount() { return list.size(); } @override public object getitem(int position) { return list.get(position); } @override public view getview(int position, view convertview, viewgroup parent) { view row = convertview; layouthandler layouthandler; if(row == null) { layoutinflater layoutinflater =(layoutinflater) this.getcontext().getsystemservice(context.layout_inflater_service); row = layoutinflater.inflate(r.layout.list_item,parent,false); layouthandler = new layouthandler(); layouthandler.name = (textview) row.findviewbyid(r.id.text_name); layouthandler.days = (textview) row.findviewbyid(r.id.days_list_item); layouthandler.hours = (textview) row.findviewbyid(r.id.hours_list_item); layouthandler.minutes = (textview) row.findviewbyid(r.id.minutes_list_item); //layouthandler.seconds = (textview) row.findviewbyid(r.id.seconds_list_item); row.settag(layouthandler); } else{ layouthandler =(layouthandler) row.gettag(); } dataprovider dataprovider =(dataprovider) this.getitem(position); layouthandler.name.settext(dataprovider.getname()); layouthandler.days.settext(dataprovider.getsdays()); layouthandler.hours.settext(dataprovider.getshours()); layouthandler.minutes.settext(dataprovider.getsminutes()); //layouthandler.seconds.settext(dataprovider.getsseconds()); return row; }
}
what supposed edit?
you should use adapter link xml layout file , list of object element respectively display items , populate listview.
then, delete item in database , refresh list.
Comments
Post a Comment