I'm attempting to add a menu to a list view. When the user long click's on a list item I want to have that menu populate (really I'm showing a list of items the user wants to buy, and the menu will only display "Remove")... This is how i'm displaying the list, how would I add the the listenener for the long click?
public static class CheckoutFragment extends ListFragment {
/**
*
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public List<Entry> cart;
JSONObject json;
jsonParser jParser = new jsonParser();
JSONArray directory;
/**
* Returns a new instance of this fragment for the 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 for 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 the 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;
}
}