开发者

get view id from oncontextitemselected

I've several buttons registered for context menu

how do I know which button was clicked for the menu to appear?

below is the pseudocode that i'll be using. I need to do something related to which button clicked (I have few more buttons to be declared), how do I know that the context menu is activated from which button click.

EDIT: I think i didn't make myself clear, I wanted to know which button was clicked for the menu to appear. Not which menu item is clicked. Anyways, I've a solution which I'll add in pretty soon.

thanks

private static final int SEND_AS_TEXT = Menu.FIRST;
private static final int SEND_AS_IMAGE = Menu.FIRST + 1;

    @Override
    public void onCreate(Bundle savedInstanceSta开发者_如何学Cte) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        sendAllBtn = (Button)findViewById(R.id.sendAllBtn);
        sendAllBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        registerForContextMenu(v);
        openContextMenu(v);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        switch(item.getItemId()){
        case SEND_AS_TEXT:
            //do sth related to the button clicked
            break;

        }
        return super.onContextItemSelected(item);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        // TODO Auto-generated method stub
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(Menu.NONE, SEND_AS_TEXT, SEND_AS_TEXT, "Send As Text");
        menu.add(Menu.NONE, SEND_AS_IMAGE, SEND_AS_IMAGE, "Send As Image");
    }


Ok, thanks alot for the help from the others which clear my doubts on the getItemId since it returns the ID that I assigned to the menu item. In my case, I wanted to know which button was clicked before the contextmenu was created.

To do this, I simply create a long variable to store the button that was clicked. The ID of the button can be obtained as in the following:

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    // TODO Auto-generated method stub
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Send As..");
    menu.add(Menu.NONE, SEND_AS_TEXT, SEND_AS_TEXT, "Send As Text");
    menu.add(Menu.NONE, SEND_AS_IMAGE, SEND_AS_IMAGE, "Send As Image");
    btnId = v.getId(); //this is where I get the id of my clicked button
}

and later on I'll only need to refer to this btnId to do whatever I want.


I think it makes more sense to use the ID of the specific view. Say you've got an ListView populated of items containing your data, but in-between some of the items you've created separators/headers. You don't want the separators to handle clicks/long clicks.

In some cases it's totally fine to just refer to "position" or MenuInfo.id, but depending on your data structure you might need more control.

What you can do is to set ID's for the items/views within your ListView (view.setId(x), where x represents the ID/position for your data structure/object. Then, when creating a ContextMenu and handling selections within it do the following to read that ID out:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
    int id = info.targetView.getId();

    // now you can refer to your data with the correct ID of yours
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    int id = info.targetView.getId();

    // now you can refer to your data with the correct ID of yours
}


If you are looking for the ID of your underlying data (provided by the adapter's getItemId(int)), then just add the following lines in the onContextItemSelected method:

final AdapterView.AdapterContextMenuInfo info = 
  (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
final long datasId = info.id    // get datas id


@Override
public boolean onContextItemSelected(MenuItem item) {
    item.getItemId();
    return super.onContextItemSelected(item);
}


try this...

@Override
public boolean onContextItemSelected(MenuItem item)
 {
    if(item.getItemId()==SEND_AS_TEXT)
    {
        //code for send text
    } 
    else if(item.getItemId()==SEND_AS_IMAGE)
    {
       //code for send image
    }
    return super.onContextItemSelected(item);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜