Include Button

Asked

Viewed 57 times

0

Good evening, I’m doing a TCC project, and I was wondering which function I can use on the buttons + and -. The idea is a request app, where when you click on + an item will be added, and when you click - an item will be removed. I am using the Andoid studio

inserir a descrição da imagem aqui

1 answer

1


"Which function to use" is something very generic. If you want to do some action when one of the buttons is pressed, you have two alternatives.

The first alternative is to create in the class of this Activity (in this case, your "Burguerking.java") an Onclicklistener on the two buttons.

For this, you will need to give a findView on the two components

private <Componente> botaoAdicionar = findViewById(R.id.<seuID>);

Then to create the Onclicklistener

botaoAdicionar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Faça o que desejar
        }
    });

To second alternative (not very recommended) is to create a function and link it to your component. For this, create the function, click your button and go to inputMethod. There, you select the function you created.

If you want to add and remove items, you will need an Arraylist. If the items you want to add have specific features, for example, nome do pedido, preço, quantidade, etc... you will need to create an object with these features and an Array of that object.

ArrayList<Pedido> listaDePedidos = new ArrayList<Pedido>();

If you’re not familiar with this, I recommend reading "Object-Oriented Programming".

Then, of course, just use the methods of your Array that add and remove items.

listaDePedidos.add(...);
listaDePedidos.remove(...);
  • Already helped me Enough, I was needing a North to implement the functions.

Browser other questions tagged

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