i have sqlite database in store events. store starttime
, endtime
against each events in date format.
i have fragments days tabs , 1 activity adds events. events shown in fragments.
now want check if events overlap each other , based on app should display message.
how display message in added event activity when user saves events.
how can this?
i have tried way still events added same stat time , end time :
imagebutton imagebutton = (imagebutton)findviewbyid(r.id.imgbtn_fab); imagebutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { eventtitle = title.gettext().tostring(); eventtablehelper db = new eventtablehelper(getapplication()); list<eventdata> events; events = db.getallevents(); (eventdata eventdata : events) { string datefrom = eventdata.getfromdate(); string[] times = datefrom.substring(11, 16).split(":"); minutesfrom = integer.parseint(times[0]) * 60 + integer.parseint(times[1]); string dateto = eventdata.gettodate(); //times = dateto.substring(11,16).split(":"); string[] times1 = dateto.substring(11, 16).split(":"); minutesto = integer.parseint(times1[0]) * 60 + integer.parseint(times1[1]); } if (eventtitle.length() == 0) { toast.maketext(getapplicationcontext(), "title can not empty.", toast.length_long).show(); } else if(minutesfrom == minutesto) { toast.maketext(getapplicationcontext(),"event same time exists.",toast.length_long).show(); } else { db.addevent(new eventdata(eventtitle, starttime, endtime, dayofweek, location)); setresult(result_ok, i); finish(); } } });
i dates this:
c = calendar.getinstance(); year = c.get(calendar.year); month = c.get(calendar.month); day = c.get(calendar.day_of_month); hour = c.get(calendar.hour_of_day); minute = c.get(calendar.minute); df = new simpledateformat("hh:mm a"); datefrom = c.gettime(); starttime = string.valueof(datefrom); atime = df.format(datefrom.gettime()); showfromtime.settext(atime); c.add(calendar.hour_of_day, 1); dateto = c.gettime(); endtime = string.valueof(dateto); btime = df.format(dateto.gettime()); showtotime.settext(btime);
i have created eventtablehelper
public class eventtablehelper extends sqliteopenhelper { private static final string table = "event"; private static final string key_id = "id"; private static final string key_title = "title"; private static final string key_from_date = "datefrom"; private static final string key_to_date = "dateto"; private static final string key_location = "location"; private static final string key_day_of_week = "dayofweek"; public eventtablehelper(context context) { super(context, constants.database_name, null, constants.database_version); } @override public void oncreate(sqlitedatabase db) { } public void createtable(sqlitedatabase db){ string create_events_table = "create table " + table+ "(" + key_id + " integer primary key," + key_title + " text," + key_from_date + " date," + key_to_date + " date," + key_day_of_week + " text " + key_location + " text" + ")"; db.execsql(create_events_table); } // upgrading database @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { db.execsql("drop table if exists " + table); // createtable(db); // oncreate(db); } public void addevent(eventdata event) { sqlitedatabase db = this.getwritabledatabase(); contentvalues values = new contentvalues(); values.put(key_title,event.gettitle()); values.put(key_from_date, event.getfromdate()); values.put(key_to_date,event.gettodate()); values.put(key_day_of_week,event.getdayofweek()); values.put(key_location,event.getlocation()); db.insert(table, null, values); db.close(); } eventdata getevent(int id) { sqlitedatabase db = this.getreadabledatabase(); eventdata eventdata = new eventdata(); cursor cursor = db.query(table, new string[]{key_id, key_title, key_from_date, key_to_date, key_location}, key_id + "=?", new string[]{string.valueof(id)}, null, null, null, null); if( cursor != null && cursor.movetofirst() ) { eventdata = new eventdata(integer.parseint(cursor.getstring(0)), cursor.getstring(1), cursor.getstring(2), cursor.getstring(3), cursor.getstring(4)); } return eventdata; } public list<eventdata> getallevents() { list<eventdata> conlist = new arraylist<eventdata>(); string selectquery = "select * " + table; sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery(selectquery, null); if (cursor.movetofirst()) { { eventdata event = new eventdata(); event.setid(integer.parseint(cursor.getstring(0))); event.settitle(cursor.getstring(1)); event.setfromdate(cursor.getstring(2)); event.settodate(cursor.getstring(3)); event.setlocation(cursor.getstring(4)); conlist.add(event); } while (cursor.movetonext()); } return conlist; } }
i tried use getall events function in activity data. please help. thankyou..
edit :: current code.
@override public void onclick(view v) { eventtitle = title.gettext().tostring(); eventtablehelper db = new eventtablehelper(getapplication()); list<eventdata> events; // events = db.getoverlappingevents(starttime,endtime); if (eventtitle.length() == 0) { toast.maketext(getapplicationcontext(), "title can not empty.", toast.length_long).show(); } else if(db.doeseventoverlap(starttime,endtime)) { toast.maketext(getapplicationcontext(), "event exists", toast.length_long).show(); } else { db.addevent(new eventdata(eventtitle, starttime, endtime, dayofweek, location)); setresult(result_ok, i); finish(); } } });
minutesto, minutesfrom have value last line in db.
i try new function in eventtablehelper:
list<eventdata> getoverlappingevents(date starttime, date endtime) { list<eventdata> conlist = new arraylist<eventdata>(); string selectquery = "select * " + table + " (" + key_from_date + " < '" + starttime + "' , '" + starttime + "' < " + key_to_date + ") or " + " (" + key_from_date + " < '" + endtime + "' , '" + endtime + "' < " + key_to_date + ") or " + " (" + key_from_date + " < '" + starttime + "' , '" + endtime + "' < " + key_to_date + ") or " + " ('" + starttime + "' < " + key_from_date + " , " + key_to_date + " < '" + endtime + "')"; sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery(selectquery, null); if (cursor.movetofirst()) { { eventdata event = new eventdata(); event.setid(integer.parseint(cursor.getstring(0))); event.settitle(cursor.getstring(1)); event.setfromdate(cursor.getstring(2)); event.settodate(cursor.getstring(3)); event.setlocation(cursor.getstring(4)); conlist.add(event); } while (cursor.movetonext()); } return conlist; }
and in onclick:
events = db.getoverlappingevents(); ...
and instead of:
else if(minutesfrom == minutesto)
this:
else if(events.size() > 0)
actually further optimized, because looks don't need overlapping events data, boolean if there's @ least 1 overlapping event:
boolean doeseventoverlap(date starttime, date endtime) { string selectquery = "select count(*) " + table + " (" + key_from_date + " < '" + starttime + "' , '" + starttime + "' < " + key_to_date + ") or " + " (" + key_from_date + " < '" + endtime + "' , '" + endtime + "' < " + key_to_date + ") or " + " (" + key_from_date + " < '" + starttime + "' , '" + endtime + "' < " + key_to_date + ") or " + " ('" + starttime + "' < " + key_from_date + " , " + key_to_date + " < '" + endtime + "')"; sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery(selectquery, null); if (cursor.movetofirst()) { { eventdata event = new eventdata(); return cursor.getint(0) > 0; } while (cursor.movetonext()); } return false; }
and in onclick instead of:
else if(minutesfrom == minutesto)
this:
else if(db.doeseventoverlap())
Comments
Post a Comment