I want get a selected item from a listview.
When maintain a longclick a listview item I get the context menu (everything works) but I want to know how to detect what is POSITION which to made the longclick
1º declare the items for a context menu in
/res/menu/NAMEOFXMLUWANT.XML
like this
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/Abrir"
android:title="@string/Abrir" />
<item android:id="@+id/Editar"
android:title="@string/Edit" />
<item android:id="@+id/Borrar"
android:title="@string/delete" />
<item android:id="@+id/Alarma"
android:title="@string/Alarma" />
</menu>
2º-AFTER in the class of u want do the context menu in LIST VIEW
lista = me listview
lista = (ListView) findViewById(R.id.Lista);
final ArrayAdapter<String> adaptador = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, ficheros);
lista.setLongClickable(true);
lista.setAdapter(adaptador);
lista.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) { //here u set u rute
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
}
});
3º AND FINALY, u set u onclick to items
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item
.getMenuInfo();
switch (item.getItemId()) {
case R.id.Editar:
System.out.println("Editar");
return true;
case R.id.Borrar:
System.out.println("borrar");
return true;
case R.id.Abrir:
System.out.println("Abrir");
return true;
case R.id.Alarma:
System.out.println("Alarma");
return true;
default:
return super.onContextItemSelected(item);
}
}
for me all this WORKS :), but i want get select item with the long click, thx for reading ^^
menuInfo.position solved my problem, courtesy @Luksprog
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With