How to differentiate buttons from a ListView?

0

I have ListView with ImageButton s on each line, and I want when I click the button it saves the product with the price, so I add in an activity. How do I get the button information from the clicked button?

    
asked by anonymous 26.07.2016 / 01:37

1 answer

3

You can associate a slightly different listener for each of these buttons directly on the Adapter. Your getView would look something like this:

public View getView(final int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = getLayoutInflater();
    View linha = inflater.inflate(R.layout.linha_de_produto, parent,
                        false);
    ImageButton carrinhoButton = (ImageButton)  linha.findViewById(R.id.carrinhoButton);
    carrinhoButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
               // Aqui entra o código que vai usar a variável position para fazer uma coisa diferente pra cada caso.
        }

    });
}
    
26.07.2016 / 02:23