android - App crashes while deleting an item from Recyclerview -
in app showing list of items in recyclerview. have create 2 funtionality edit , delete each of item in recyclerview. app user can create list of task , can edit item, delete item recyclerview. if click edit, working perfectly. if click delete option app crashes message array index out of rance exception in adaper class.
here code adapter. have mentioned line in code , getting app crashes.
edited adapter class
public class todoadapter extends realmrecyclerviewadapter<todomodel, todoadapter.taskholder> { public final static string intent_key_id = "taskid"; public final static string intent_key_position = "position"; public final static string date_format = "dd/mmm/yy"; private realm realm; public interface tasklistener { } private final tasklistener tasklistener; private final context context; public todoadapter(tasklistener tasklistener, realmresults<todomodel> realmresults, context context, realm realm) { super(realmresults, true); this.tasklistener = tasklistener; this.context = context; this.realm = realm; } @override public taskholder oncreateviewholder(viewgroup parent, int viewtype) { return new taskholder( layoutinflater.from(parent.getcontext()).inflate(r.layout.todo_row, parent, false)); } @override public void onbindviewholder(taskholder holder, final int position) { final todomodel task = getdata().get(position); holder.tasktextview.settext(task.getname()); holder.donecheckbox.setchecked(task.isdone()); holder.timetextview.settext( task.gettime() ); final simpledateformat sdf = new simpledateformat(date_format); final date date = (task.getdate()); if (sdf.format(date).equals(sdf.format(getdate(0)))) holder.datetextview.settext("today"); else if (sdf.format(date).equals(sdf.format(getdate(-1)))) holder.datetextview.settext("yesterday"); else if (sdf.format(date).equals(sdf.format(getdate(1)))) holder.datetextview.settext("tomorrow"); else if (date.gettime() < getdate(6).gettime()) { calendar calendar = calendar.getinstance(); calendar.settime(date); holder.datetextview.settext( dateformat.format("eeee", calendar.gettime()).tostring()); } else holder.datetextview.settext(sdf.format(task.getdate())); holder.cardview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { intent intent = new intent(context, todoaddactivity.class); intent.addflags(intent.flag_activity_new_task); intent.putextra(intent_key_id, gettask(position).getid()); context.startactivity(intent); } }); holder.donecheckbox.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { todomodel task = new todomodel(); task.setid(gettask(position).getid()); task.setdate(gettask(position).getdate()); task.setname(gettask(position).getname()); task.setdone(((checkbox) v).ischecked()); updatetask(position, task); } }); holder.deleteimageview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (gettask(position).isdone()) { removetask(position); notifydatasetchanged(); return; } alertdialog.builder alertdialog = new alertdialog.builder(context); alertdialog.settitle("confirm delete?"); alertdialog.setmessage("do want delete task created?"); alertdialog.setpositivebutton("yes", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.cancel(); removetask(position); } }); alertdialog.setnegativebutton("no", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.cancel(); } }); alertdialog.show(); } }); } public class taskholder extends recyclerview.viewholder { public cardview cardview; public textview tasktextview; public textview datetextview; public textview timetextview; public imageview deleteimageview; public checkbox donecheckbox; public taskholder(view itemview) { super(itemview); tasktextview = (textview) itemview.findviewbyid( r.id.row_task_list_tv_name); datetextview = (textview) itemview.findviewbyid(r.id.row_task_list_tv_date); timetextview=(textview)itemview.findviewbyid( r.id.row_task_list_tv_time ); deleteimageview = (imageview) itemview.findviewbyid(r.id.row_task_list_iv_delete); donecheckbox = (checkbox) itemview.findviewbyid(r.id.row_task_list_cb_done); cardview = (cardview) itemview.findviewbyid(r.id.cardview); } } private date getdate(int day) { calendar calendar = calendar.getinstance(); calendar.add(calendar.date, day); return calendar.gettime(); } protected todomodel gettask(int position) { return getdata().get(position); } protected void updatetask(int position, todomodel task) { realm.begintransaction(); todomodel newtask = realm.where(todomodel.class).equalto("id", task.getid()).findfirst(); newtask.setdate(task.getdate()); newtask.setdone(task.isdone()); newtask.setname(task.getname()); realm.committransaction(); } protected void removetask(int position) { todomodel newtask = realm.where(todomodel.class).equalto("id", gettask(position).getid()).findfirst(); realm.begintransaction(); newtask.deletefromrealm(); realm.committransaction(); notifyitemremoved(position); } }
the crash report
09-10 12:25:57.876 7332-7332/realmtest.com.to_do_list_test e/androidruntime: fatal exception: main process: realmtest.com.to_do_list_test, pid: 7332 java.lang.arrayindexoutofboundsexception: out of range in /users/cm/realm/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_collection.cpp line 133(requested: 1 valid: 1) @ io.realm.internal.collection.nativegetrow(native method) @ io.realm.internal.collection.getuncheckedrow(collection.java:386) @ io.realm.orderedrealmcollectionimpl.get(orderedrealmcollectionimpl.java:106) @ io.realm.realmresults.get(realmresults.java:53) @ io.realm.orderedrealmcollectionimpl.get(orderedrealmcollectionimpl.java:19) @ realmtest.com.to_do_list_test.activity.todoadapter.gettask(todoadapter.java:157) @ realmtest.com.to_do_list_test.activity.todoadapter$3.onclick(todoadapter.java:106) @ android.view.view.performclick(view.java:5610) @ android.view.view$performclick.run(view.java:22265) @ android.os.handler.handlecallback(handler.java:751) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:154) @ android.app.activitythread.main(activitythread.java:6077) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:866) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:756)
implementation of getdata()
@nullable public orderedrealmcollection<t> getdata() { return adapterdata; }
taskactivity class
public class taskactivity extends appcompatactivity implements todoadapter.tasklistener{ private static final int add_task_request_code = 1000; private static final int edit_task_request_code = 1001; private realm realm; private recyclerview recyclerview; private todoadapter adapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.fragment_todo_layout); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); toolbar.settitle("todo list"); setsupportactionbar(toolbar); realm = realm.getdefaultinstance(); recyclerview =(recyclerview)findviewbyid(r.id.activity_tasks_ll_task); setuprecycler(); // variables // views floatingactionbutton addfloatingactionbutton = (floatingactionbutton) findviewbyid(r.id.activity_tasks_fab_add); //listeners addfloatingactionbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { startactivityforresult(new intent(getapplicationcontext(), todoaddactivity.class), add_task_request_code); } }); } private void setadapter() { adapter = new todoadapter(this, realm.where(todomodel.class).findall(),this,realm); recyclerview.setadapter(adapter); adapter.notifydatasetchanged(); } private void setuprecycler() { // use setting improve performance if know changes // in content not change layout size of recyclerview recyclerview.sethasfixedsize(true); // use linear layout manager since cards vertically scrollable final linearlayoutmanager layoutmanager = new linearlayoutmanager(this); layoutmanager.setorientation(linearlayoutmanager.vertical); recyclerview.setlayoutmanager(layoutmanager); setadapter(); } }
remove return statement if (gettask(position).isdone()) {
, put alertdialog
in else-clause of if, seen below.
holder.deleteimageview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (gettask(position).isdone()) { removetask(position); notifydatasetchanged(); }else { alertdialog.builder alertdialog = new alertdialog.builder(context); alertdialog.settitle("confirm delete?"); alertdialog.setmessage("do want delete task created?"); alertdialog.setpositivebutton("yes", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.cancel(); removetask(position); } }); alertdialog.setnegativebutton("no", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { dialog.cancel(); } }); alertdialog.show(); } } });
Comments
Post a Comment