Problems with multicones in the android menu

0

I'm trying to create two buttons but it's only working when I click on one of the buttons.

Myactivitylookslikethis:

<?xmlversion="1.0" encoding="utf-8"?>
  <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools" >
  <item
    android:id="@+id/comentar"
    android:icon="@drawable/comentarios"
    android:title="Verificar todos comentarios do estabelecimento"
    app:showAsAction="ifRoom" />
  <item
    android:id="@+id/favoritar"
    android:icon="@drawable/ic_favoritar"
    android:title="Adicionar estabelecimento aos favoritos"
    app:showAsAction="ifRoom"/>
</menu>

Already in my class it looks like this:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_favoritar_restaurante, menu);
    menuItemCoracao = menu.getItem(0);

    if (restaurante.isFavorito()) {
        // Altera o icone para favoritado
        menuItemCoracao.setIcon(R.drawable.ic_favoritado);
    }

    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (!restaurante.isFavorito()) {
        // Adiciona aos favoritos
        adicionarAosFavoritos();
    } else {
        // Remove
        removerDosFavoritos();
    }
    return super.onOptionsItemSelected(item);
}

The logic of the first one is working perfectly, but now I need to create the logic of the second one, where I will send it to a new java that will search the comments of that respective client.

How to do this? Where am I going wrong?

    
asked by anonymous 16.05.2017 / 20:41

1 answer

1

Renan, you need to check the clicked ID, eg:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if(id == R.id.favoritar){
       if (!restaurante.isFavorito()) {
         // Adiciona aos favoritos
         adicionarAosFavoritos();
       } else {
         // Remove
         removerDosFavoritos();
       }
    }
    else if(id == R.id.comentar)
    {
       vaiProsComentarios();
    }

    return super.onOptionsItemSelected(item);
}
    
16.05.2017 / 20:44