Android delete selected items listview using contextmenu actionbar -
so far i've tried:
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(); // todo app madapter.removeselection(position); 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; } }
but need know how selected items , put inside onactionitemclicked i've tried here:
@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(); // todo app madapter.removeselection(position); mode.finish(); } return true; }
i commented out madapter.removeselection() because don't know put inside.
any ideas? pretty appreciated. thanks.
their multiple things do.. easiest way think follows:
it doesn't work because onactionitemclicked expects 1 item. select multiple items. easiest way create loop in handle normal procedure onactionitemclicked expects
listview mlistview = getlistview(); @override public boolean onactionitemclicked(actionmode mode, menuitem item) { // todo auto-generated method stub switch (item.getitemid()) { case r.id.item_delete: nr = 0; long[] longconverts = new long(mlistview.getcheckeditemids()); private list<long> itemids = new arraylist<long>(arrays.aslist(longconverts)); for(int i=0;i<itemids.size();i++){ mlistview.remove(itemids.get(i)); madapter.clearselection(); // todo app madapter.removeselection(position); mode.finish(); } return true; }
i think should work.. retrieves selected items first listview
put them in array , loops on array remove items 1 one..
i hope works you
Comments
Post a Comment