John Donvan
John Donvan

Reputation: 566

how to get text of the view in onContextItemSelected android

I am developing an app in which i create some labels programmatically. I add context menu to each label so when the user click on any label he can delete it.

I am using the following code to create the labels and register the context menu.

 private void drawLabels(HashMap<String, String> contact, int position)
    {
        FlowLayout flowLayout = (FlowLayout)findViewById(R.id.recipients);
        TextView contactLabel = (TextView)getLayoutInflater().inflate(R.layout.contactlabeltemplate, null);
        contactLabel.setText(contact.get("name"));
        contactLabel.setTag(R.id.number,String.valueOf( contact.get("number")));
        contactLabel.setTag(R.id.position,Integer.valueOf(position));
        flowLayout.addView(contactLabel);
        registerForContextMenu(contactLabel);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
    {
        String name = ((TextView)v).getText().toString();
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle(name);
        menu.add(0, v.getId(), 0, "Call");
        menu.add(0, v.getId(), 0, "SMS");


    }

    @Override
    public boolean onContextItemSelected(MenuItem item){

        if(item.getTitle()=="Call"){

            Toast.makeText(getApplicationContext(),"text view text",Toast.LENGTH_LONG).show();
        }
        else if(item.getTitle()=="SMS"){
            Toast.makeText(getApplicationContext(),"sending sms code",Toast.LENGTH_LONG).show();
        }else{
            return false;
        }
        return true;
    }

I want to show the textview text in onContextItemSelected. How to do so ?

Upvotes: 0

Views: 1107

Answers (1)

matiash
matiash

Reputation: 55340

Since you're setting the menu item id to the same id of the view, you can use that to locate it, i.e.

TextView tv = (TextView)findViewById(item.getItemId());

Upvotes: 1

Related Questions