How to add menu to Android List item via long click -
i'm attempting add menu list view. when user long click's on list item want have menu populate (really i'm showing list of items user wants buy, , menu display "remove")... how i'm displaying list, how add the listenener long click?
public static class checkoutfragment extends listfragment { /** * * fragment argument representing section number * fragment. */ private static final string arg_section_number = "section_number"; public list<entry> cart; jsonobject json; jsonparser jparser = new jsonparser(); jsonarray directory; /** * returns new instance of fragment given section number. */ public static fragment newinstance(int sectionnumber) { checkoutfragment fragment = new checkoutfragment(); bundle args = new bundle(); args.putint(arg_section_number, sectionnumber); fragment.setarguments(args); return fragment; } public checkoutfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { cart = dashboard.datasource.getallproducts(); view v = inflater.inflate(r.layout.fragment_checkout, container, false); button order = (button) v.findviewbyid(r.id.order); order.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(v.getcontext(), "order submitted", toast.length_short).show(); submitorder(); flag = false; } }); checkoutarrayadapter checkoutadapter = new checkoutarrayadapter( container.getcontext(), r.layout.fragment_checkout, r.layout.checkout_list_item, cart); setlistadapter(checkoutadapter); dashboard.datasource = displayproduct.datasource; getlistview().setonitemlongclicklistener(new adapterview.onitemlongclicklistener() { @override public boolean onitemlongclick(adapterview<?> parent, view view, int position, long id) { return false; // show menu item in position. } }); return v; } protected void submitorder() { ordertask order = new ordertask(); order.execute(); } @override public void onattach(activity activity) { super.onattach(activity); ((dashboard) activity).onsectionattached(getarguments().getint( arg_section_number)); } public class checkoutarrayadapter extends arrayadapter<entry> { private final layoutinflater minflater; private list<entry> cart; public checkoutarrayadapter(context context, int listviewlayout, int listitemlayout, list<entry> cartitems) { super(context, listviewlayout, listitemlayout, cartitems); minflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); cart = cartitems; } /** * populate new items in list. */ @override public view getview(int position, view convertview, viewgroup parent) { view view; if (convertview == null) { view = minflater.inflate(r.layout.checkout_list_item, parent, false); } else { view = convertview; } entry item = getitem(position); ((textview) view.findviewbyid(r.id.cart_entry)).settext(item .getqty() + "x " + item.getname() + "\t" + item.getsku() + " "); return view; } }
you can listview's setonitemlongclicklistener()
. this:
getlistview().setonitemlongclicklistener(new adapterview.onitemlongclicklistener() { @override public boolean onitemlongclick(adapterview<?> parent, view view, int position, long id) { // show menu item in position. } });
at end of onviewcreated()
appropriate time execute code, i.e.
@override public void onviewcreated(view view, bundle savedinstancestate) { super.onviewcreated(view, savedinstancestate); getlistview().setonitemlongclicklistener(...); }
Comments
Post a Comment