ID del elemento en el que se hizo clic - ContextMenu de Android

Gday,
I have a ListView populated with various text values, and I want to have it that when you long press and open the context menu, you can copy the text in the ListItem you long pressed. So far I've got the context menu to pop up with the "Copy" option:

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
    //this was following another question but I don't know what to do with it
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
    long selectedId = info.id;
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context, menu);
}
public boolean onContextItemSelected(MenuItem item)
{
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    switch (item.getItemId())
    {
    case R.id.copy:
        //used to be in a function but wasn't sure about views
        //yes I know it's depreciated but it works ;)
        ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        TextView clicked = (TextView)this.findViewById(???);
        clipboard.setText(clicked.getText());
        Context context = getApplicationContext();
        Toast copied = Toast.makeText(context, "Story copied to clipboard.", Toast.LENGTH_LONG);
        copied.show();
        return true;
    default:
        return super.onContextItemSelected(item);
    }
}

Muchas Gracias

preguntado el 08 de noviembre de 11 a las 10:11

@Chirag I don't know what to put where the ??? is. If I could pass the ID of the item long-pressed, I could just do ...findViewById(id); -

2 Respuestas

Set up a variable to hold the view that gets clicked:

View clicked;

Then assign a value to it when the context menu is created on it:

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
    clicked = v;

    //this was following another question but I don't know what to do with it
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
    long selectedId = info.id;
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context, menu);
}

And now you can use it on your final method:

public boolean onContextItemSelected(MenuItem item)
{
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    switch (item.getItemId())
   {
    case R.id.copy:
        //used to be in a function but wasn't sure about views
        //yes I know it's depreciated but it works ;)
        ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);

        clipboard.setText(clicked.getText());
        // this should work now properly.

        Context context = getApplicationContext();
        Toast copied = Toast.makeText(context, "Story copied to clipboard.", Toast.LENGTH_LONG);
        copied.show();
        return true;
    default:
        return super.onContextItemSelected(item);
    }
}

respondido 08 nov., 11:15

when I put this in, Eclipse says it's an error and offers to change it to item.getTitle(), however that just puts the "Copy" (title of the context menu I touched) into the clipboard, whereas I wanted the text of what was in the ListItem I originally long-pressed - ProfSmiles

Sorry, my bad. You could store the proper view in a variable when the onCreateContextMenu is called. I'll edit my answer. - Iiro Krankka

Using the above code, the clicked.getText() is apparently an error: "The method getText() is undefined for the type View" - ProfSmiles

I think you have answered your own question. The id is:

long selectedId = info.id;

Respondido 02 Abr '14, 16:04

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.