android - MultiChoiceModeListener GridView selection not changing background color -
i'm following this tutorial implement multiple select in grid view, far can select items not changing background color, i've debugged code, , seems mselection null, don't know why happening
public class gridadapter extends baseadapter { private context mcontext; private arraylist<child> child; imageloader imageloader = appcontroller.getinstance().getimageloader(); private hashmap<integer, boolean> mselection; public gridadapter(context context, arraylist<child> childvalues) { mcontext = context; mselection = new hashmap<integer, boolean>(); child = childvalues; } @override public int getcount() { return child.size(); } @override public object getitem(int position) { return position; } @override public long getitemid(int arg0) { return 0; } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder = null; networkimageview i; if (convertview == null) { layoutinflater inflater = (layoutinflater) mcontext .getsystemservice(context.layout_inflater_service); convertview = inflater.inflate(r.layout.child_item, null); holder = new viewholder(); if (imageloader == null) imageloader = appcontroller.getinstance().getimageloader(); = (networkimageview) convertview .findviewbyid(r.id.flag); i.setimageurl(string.valueof(child.get(position).getimage()), imageloader); if (mselection.get(position) != null) { convertview.setbackgroundresource(r.color.bg);// selected position make red } convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } holder.text = (textview) convertview.findviewbyid(r.id.label); holder.text.settext(child.get(position).getname()); return convertview; } static class viewholder { textview text; } 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(); } }
expandable adapter:
adapter = new gridadapter(context, listchild); gridview.setadapter(adapter);// adapter gridview.setchoicemode(gridview.choice_mode_multiple_modal); gridview.setmultichoicemodelistener(new multichoicemodelistener() { private int nr = 0; @override public void onitemcheckedstatechanged(actionmode mode, final int position, long id, boolean checked) { if (checked) { nr++; adapter.setnewselection(position, checked); } else { nr--; adapter.removeselection(position); } mode.settitle(nr + " rows selected!"); } @override public boolean oncreateactionmode(actionmode mode, menu menu) { mode.settitle("select items"); return true; } @override public boolean onprepareactionmode(actionmode mode, menu menu) { return false; } @override public boolean onactionitemclicked(actionmode mode, menuitem item) { stringbuilder sb = new stringbuilder(); set<integer> positions = adapter.getcurrentcheckedposition(); (integer pos : positions) { sb.append(" " + pos + ","); } return false; } @override public void ondestroyactionmode(actionmode mode) { nr = 0; adapter.clearselection(); } }); gridview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { gridview.setitemchecked(position, !adapter.ispositionchecked(position)); } }); return convertview; }
Comments
Post a Comment