tags:

views:

280

answers:

3
 lv.setOnItemClickListener(new OnItemClickListener() {
             @Override
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
              TextView text = (TextView) view.findViewById(R.id.btitle);
              registerForContextMenu(text);
              view.showContextMenu();
              }
            });
       }

     @Override
     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
       super.onCreateContextMenu(menu, v, menuInfo);
       TextView text = (TextView) v.findViewById(R.id.btitle);
       CharSequence itemTitle = text.getText();
       menu.setHeaderTitle(itemTitle);

       MenuInflater inflater = getMenuInflater();
       inflater.inflate(R.menu.context_menu, menu);

     }

Hello,

I'm trying to open a contextMenu on short item click. I've managed to do so only if I add registerForContextMenu(getListView()); somewhere but this also triggers contextMenu on long click (which I don't want to happen).

Tried view.showContextMenu() but it doesn't do anything unless i add the registerForContextMenu(getListView()); . tried registering the clicked item first and then call the showContextMenu() but didn't do anything as well...

Also, I want to get the clicked item image + text so I can use them in the contextMenu.

Appreciate the help!

A: 

I believe that if you add an OnLongClickListener to the view, you should be able to prevent it from showing the context menu on long click.

In onItemClick the view parameter is the item view, so you should be able to get ImageViews or TextViews from that:

view.findById(R.id.my_image_view);
Mayra
Hi Mayra,I know... but for some reason it's not getting this. no force close.. just shows the details of the first item in the list.
liorry
"its not getting this"?
Mayra
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); TextView text = (TextView) v.findViewById(R.id.btitle); menu.setHeaderTitle(text.getText());........}Gets only the first item title and not the clicked item.
liorry
You should be doing that in onItemClick, not onCreateContextMenu.
Mayra
I should be setting a contextMenu title in onItemClick???? before the contextMenu is even created?menu.setHeaderTitle is only possible under onCreateContextMenu.
liorry
Hmm, so the view that gets passed into onCreateContextMenu is the first item in the ListView? I think you might be confusing the context menu methods for the activity, and for the list. You could try using list's showContextMenuForChild to display the context menu instead.
Mayra
Nope... read about it at Android API documentation... and tried it.. didn't help.I've changed it to long click opens the contextMenu and single click allows item sorting but that didn't help either.. still showing only the details of the first item in the list.:/
liorry
Solved it. Thanks anyways! :)
liorry
A: 

Solution:

@Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
      super.onCreateContextMenu(menu, v, menuInfo);
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.context_menu, menu);

      AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
      long itemID = info.position;
      menu.setHeaderTitle("lior" + itemID);
    }

the "AdapterView.AdapterContextMenuInfo info (AdapterView.AdapterContextMenuInfo)menuInfo;" gives you more details about the list item clicked. Then you can use info.id, info.position and so on to retrieve the details and use them actions (edit, delete...).

liorry
A: 

fantastic! it worked pretty well. thanks for the solution liorry

OliverDroid
Glad it helped! You can mark it as as helpful if you want :-)
liorry