Call method from fragment android -
i try call method fragment activity.
public class fragmenthome extends fragment { private string database_name = "alarm.sqlite"; private sqlitedatabase database; listview listview; arraylist<alarm> list; adapteralarm adapteralarm; intent intent; alarmmanager alarmmanager; pendingintent pendingintent; switch aswitch; @requiresapi(api = build.version_codes.m) @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_fragment_home, container, false); listview = (listview)view.findviewbyid(r.id.lv); //khoi tao arraylist va adapter list = new arraylist<alarm>(); adapteralarm = new adapteralarm(list, getactivity()); //set adapter len listview listview.setadapter(adapteralarm); //đăng ký context menu registerforcontextmenu(view.findviewbyid(r.id.lv)); return view; } @override public void onstart() { super.onstart(); getdata(); } @override public void oncreatecontextmenu(contextmenu menu, view v, contextmenu.contextmenuinfo menuinfo) { super.oncreatecontextmenu(menu, v, menuinfo); this.getactivity().getmenuinflater().inflate(r.menu.menu_listview_home, menu); } @override public boolean oncontextitemselected(menuitem item) { adapterview.adaptercontextmenuinfo info = (adapterview.adaptercontextmenuinfo) item.getmenuinfo(); alarm al = list.get(info.position); int id = al.getid(); switch(item.getitemid()) { case r.id.edit: //sửa báo thức intent in = new intent(getactivity(), editactivity.class); bundle b = new bundle(); b.putint("id", id); in.putextra("data", b); startactivity(in); break; case r.id.delete: //xóa báo thức deleteitem(id); break; } return true; } public void getdata(){ //doc du lieu tu db len arraylist database = database.initdatabase(getactivity(), database_name); cursor cursor = database.rawquery("select*from alarm", null); list.clear(); for(int i=0; i<cursor.getcount(); i++) { //di chuyển cursor đến vị trí thứ cursor.movetoposition(i); //thêm đối tượng vào list list.add(new alarm(cursor.getint(0), cursor.getstring(1), cursor.getstring(2) , cursor.getint(3), cursor.getint(4), cursor.getint(5))); //lấy giờ từ db ra, tách giờ, phút alarm al = list.get(i); int hour = 0, minute=0; (int j=0; j<al.gettime().length(); j++){ //lấy giờ string ch1 = string.valueof(al.gettime().charat(0)); string ch2 = string.valueof(al.gettime().charat(1)); string t; if(ch2.equals(":")){ t = ch1; }else{ t=ch1+ch2; } hour = integer.parseint(t); //lấy phút string ch3 = string.valueof(al.gettime().charat(3)); string ch4 = string.valueof(al.gettime().charat(4)); //kiem tra neu cuoi cung cua minute la " " thi chi lay ki tu thu 3 string m; if(ch4.equals(" ")){ m =ch3; }else{ m =ch3+ch4; } minute = integer.parseint(m); } if(cursor.getint(5)==1){ setalarm(hour, minute, getactivity()); } } //bat su kien khi co thay doi du lieu tren adapter adapteralarm.notifydatasetchanged(); } public void setalarm(int hour, int minute, context context){ //khởi tạo đối tượng calender calendar calendar = calendar.getinstance(); //lấy giờ hiện tại của hệ thống calendar.settimeinmillis(system.currenttimemillis()); //đặt giờ báo thức calendar.set(calendar.hour_of_day, hour); calendar.set(calendar.minute, minute); calendar.set(calendar.second, 0); //khởi tạo intent intent = new intent(getcontext(), alarm_receiver.class); intent.putextra("extra", "yes"); //khởi tạo đối tượng pending intent pendingintent = pendingintent.getbroadcast(getcontext(), 0, intent, 0); //khởi tạo đối tường alarmmanager alarmmanager = (alarmmanager) context.getsystemservice(context.alarm_service); //đặt báo thức lặp lại hằng ngày alarmmanager.setrepeating(alarmmanager.rtc_wakeup,calendar.gettimeinmillis(), alarmmanager.interval_day, pendingintent); } public void cancelalarm(){ if(alarmmanager!=null){ intent.putextra("extra", "no"); alarmmanager.cancel(pendingintent); getactivity().sendbroadcast(intent); } } private void deleteitem(final int id){ alertdialog.builder builder = new alertdialog.builder(getactivity()); builder.settitle("confirm"); builder.setmessage("are sure, want delete?"); builder.setnegativebutton("no", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { // todo auto-generated method stub dialog.dismiss(); } }); builder.setpositivebutton("yes", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface arg0, int arg1) { // todo auto-generated method stub database = database.initdatabase(getactivity(), database_name); database.delete("alarm", "id = ?", new string[]{id+""}); //doc lại dữ liệu cho listview database = database.initdatabase(getactivity(), database_name); cursor cursor = database.rawquery("select*from alarm", null); list.clear(); for(int i=0; i<cursor.getcount(); i++){ //di chuyển cursor đến vị trí thứ cursor.movetoposition(i); //thêm đối tượng vào list list.add(new alarm(cursor.getint(0), cursor.getstring(1), cursor.getstring(2) , cursor.getint(3), cursor.getint(4), cursor.getint(5))); } //bat su kien khi co thay doi du lieu tren adapter adapteralarm.notifydatasetchanged(); toast.maketext(getactivity(), "delete sucessfully!", toast.length_short).show(); } }); builder.seticon(r.drawable.logo); builder.create().show(); } @override public void onresume() { super.onresume(); } }
i try call method cancelalarm()
fragmenthome
in activity
fragmenthome fh = new fragmenthome(); fh.cancelalarm();
but doesn't work?
it looks problem never initialize alarmmanager variable, null when code gets hit nothing happens.
public void cancelalarm(){ if(alarmmanager!=null){ ...
based on code, seems must call setalarm() before call cancelalarm().
try like:
fragmenthome fh = new fragmenthome(); fh.setalarm(12, 30, this); fh.cancelalarm();
i suggest take time learn set breakpoints , run app in debug mode. errors easy spot.
Comments
Post a Comment