Problems with multicones in the android menu

Asked

Viewed 29 times

0

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

inserir a descrição da imagem aqui

My Activity is as follows:

<?xml version="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 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 is working perfectly, but now I need to create the logic of the second, where I will send to a new java that will do the comments search of that respective client.

How to do this ? Where am I going wrong ?

1 answer

1


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

@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);
}
  • The problem is that now you’re changing the wrong icon

  • no item onCreateOptionsMenu

  • Try putting 1 instead of 0 on menu.getItem(0);

  • I did it here and it worked

  • vlw, Aja accept your answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.