Установите переменную для хранения вида, по которому щелкают:
View clicked;
Затем присвойте ему значение при создании контекстного меню:
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);
}
А теперьВы можете использовать его в вашем последнем методе:
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);
}
}