Q:

LibGDX: Problems with Button Listener in a Dialog

I'm developing a game where the player has an inventory, and you can find chests in the map with items which you can collect, so I created a class that extends from Dialog to show both item lists, player's inventory and chest's inventory, just like this:
private class InventoryDialog extends Dialog{
    public InventoryDialog(boolean isPlayerInventory, Inventory inventory) {
        super(isPlayerInventory? "Inventory":"Chest", mySkin);

        final List inventoryList = new List(mySkin);
        inventoryList.setItems(inventory.getItems());
        ScrollPane pane = new ScrollPane(inventoryList,mySkin);
        getContentTable().add(pane).size(myDimensions);
        button("Back");

        if(isPlayerInventory){
            button("Use").addListener(new ClickListener(){
                @Override
                public void clicked(InputEvent event, float x, float y) {
                    super.clicked(event, x, y);
                    player.getInventory().useItemAt(inventoryList.getSelectedIndex());

                }});
        }else{
            button("Get").addListener(new ClickListener(){
                @Override
                public void clicked(InputEvent event, float x, float y) {
                    super.clicked(event, x, y);
                    player.getInventory().add(inventory.remove(inventoryList.getSelectedIndex()));
                }});}

    }

}

So they will use or get the item only if they click that button.
My problem is that the get/use listener is always invoked even if the clicked button is the "Back" button, what am I doing wrong? :/

A:

The problem is, that I should not add a listener when I'm using the "button()" method, the correct way to use this method is, simply add the button, and as a second argument, you pass an object to recognize your button, once in the result method, you use an if to compare the object passed to know which button you clicked, just like this:
private class InventoryDialog extends Dialog{
public InventoryDialog(boolean isPlayerInventory, Inventory inventory) {
    super(isPlayerInventory? "Inventory":"Chest", mySkin);

    final List inventoryList = new List(mySkin);
    inventoryList.setItems(inventory.getItems());
    ScrollPane pane = new ScrollPane(inventoryList,mySkin);
    getContentTable().add(pane).size(myDimensions);
    button("Back");

    if(isPlayerInventory) button("Use","use");       
    else button("Get","get");

}

@Override
    protected void result(Object object) {
        super.result(object);
        if(object.equals("use")){
           player.getInventory().useItemAt(inventoryList.getSelectedIndex());
        }else if(object.equals("get")
           player.getInventory().add(inventory.remove(inventoryList.getSelectedIndex()));
        }
    }
}

