我有一个来自Android应用程序的项目,我希望当我点击contextMenu中的选项时,我会看到该位置的字符串。
String[] datos = {"Victor","Silvia","Manolo","Carlos","Ana"};
ArrayAdapter<String> adaptador = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, datos);
milista.setAdapter(adaptador);
这是通过单击上下文菜单的内容来执行某些操作的方法
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()){
case R.id.action_mostrar:
Toast.makeText(getApplicationContext(),info.position+": Opción mostrar" , Toast.LENGTH_LONG).show();
return true;
case R.id.action_eliminar:
Toast.makeText(getApplicationContext(), info.position+": Opción eliminar", Toast.LENGTH_LONG).show();
return true;
default:
return false;
}
}
我该怎么做?
修改
问题解决了,我把代码解决了:
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
String selectedWord = ((TextView) info.targetView).getText().toString();
switch (item.getItemId()){
case R.id.action_mostrar:
Toast.makeText(getApplicationContext(),selectedWord+": Opción mostrar" , Toast.LENGTH_LONG).show();
return true;
case R.id.action_eliminar:
Toast.makeText(getApplicationContext(), selectedWord+": Opción eliminar" , Toast.LENGTH_LONG).show();
return true;
default:
return false;
}
}
谢谢
答案 0 :(得分:0)
您也可以使用该位置从字符串数组中检索字符串,如下所示
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()){
case R.id.action_mostrar:
Toast.makeText(getApplicationContext(),datos[info.position]+": Opción mostrar" , Toast.LENGTH_LONG).show();
return true;
case R.id.action_eliminar:
Toast.makeText(getApplicationContext(), datos[info.position]+": Opción eliminar" , Toast.LENGTH_LONG).show();
return true;
default:
return false;
}
}