listview - Android delete selected item using contextual actionbar -
so far, have managed follow this tutorial , run successfully. wanna delete selected items on listview. code i'm using:
private string[] data = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine","ten"}; private selectionadapter madapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); madapter = new selectionadapter(this, r.layout.row_list_item, r.id.textview1, data); setlistadapter(madapter); getlistview().setchoicemode(listview.choice_mode_multiple_modal); getlistview().setmultichoicemodelistener(new multichoicemodelistener() { private int nr = 0; @override public boolean onprepareactionmode(actionmode mode, menu menu) { // todo auto-generated method stub return false; } @override public void ondestroyactionmode(actionmode mode) { // todo auto-generated method stub madapter.clearselection(); } @override public boolean oncreateactionmode(actionmode mode, menu menu) { // todo auto-generated method stub nr = 0; menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.contextual_menu, menu); return true; } @override public boolean onactionitemclicked(actionmode mode, menuitem item) { // todo auto-generated method stub switch (item.getitemid()) { case r.id.item_delete: nr = 0; madapter.clearselection(); mode.finish(); } return true; } @override public void onitemcheckedstatechanged(actionmode mode, int position, long id, boolean checked) { // todo auto-generated method stub if (checked) { nr++; madapter.setnewselection(position, checked); } else { nr--; madapter.removeselection(position); } mode.settitle(nr + " selected"); } }); getlistview().setonitemlongclicklistener(new onitemlongclicklistener() { @override public boolean onitemlongclick(adapterview<?> arg0, view arg1, int position, long arg3) { // todo auto-generated method stub getlistview().setitemchecked(position, !madapter.ispositionchecked(position)); return false; } }); } private class selectionadapter extends arrayadapter<string> { private hashmap<integer, boolean> mselection = new hashmap<integer, boolean>(); public selectionadapter(context context, int resource, int textviewresourceid, string[] objects) { super(context, resource, textviewresourceid, objects); } public void setnewselection(int position, boolean value) { mselection.put(position, value); notifydatasetchanged(); } public boolean ispositionchecked(int position) { boolean result = mselection.get(position); return result == null ? false : result; } public set<integer> getcurrentcheckedposition() { return mselection.keyset(); } public void removeselection(int position) { mselection.remove(position); notifydatasetchanged(); } public void clearselection() { mselection = new hashmap<integer, boolean>(); notifydatasetchanged(); } @override public view getview(int position, view convertview, viewgroup parent) { view v = super.getview(position, convertview, parent);//let adapter handle setting row views v.setbackgroundcolor(getresources().getcolor(android.r.color.background_light)); //default color if (mselection.get(position) != null) { v.setbackgroundcolor(getresources().getcolor(android.r.color.holo_blue_light));// selected position make red } return v; } }
question how delete selected item/s? can't figure out , if of knows , can help, i'd gladly appreciate it. thanks.
you can set onlongclicklistener
on items in listview gets item long pressed.
afterwards can reference/remove object in onactionitemclicked
.
Comments
Post a Comment