Configuring imagebutton in listview

Asked

Viewed 106 times

1

How can I set buttons inside the listview, knowing that it creates a button whenever I add an image and a textview to the string array

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 add_car = (ImageButton)  linha.findViewById(R.id.carrinhoButton);
    add_car.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
               // não sei como posso falar para o button que
               //ele deve pegar o textview que esta do seu lado na listview
               // e jogar pra uma activity, activity dos itens que o cliente selecionou
        }

    });
        return linha
}

1 answer

3


Follow an example!

/**
 * Criamos um ArrayAdapter com o tipo de Objeto que vamos trabalhar!
 */
class MeuAdapter extends ArrayAdapter<Objeto>{

    public MeuAdapter(Context context, int resource) {
        super(context, resource);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //Pegamos o objeto que vamos trabalhar!
        // Ele de ser declarado como final para que possamos usar dentro do OnClickListener!
        final Objeto objetoCorrente = getItem(position);

        final View linha = getLayoutInflater().inflate(R.layout.spinner_adapter, parent, false);
        ImageButton add_car = (ImageButton)  linha.findViewById(R.id.carrinhoButton);
        add_car.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                final Intent intent = new Intent(getApplicationContext(), ActivityModel.class );
                intent.putExtra("ID_PRODUTO", objetoCorrente.getId());
                getApplicationContext().startActivity(intent);
            }

        });


        return linha;
    }
}
  • And how would I pick it up at the other Activity? la I would form a listview tbm, just with the selected

  • 1

    getIntent(). getIntExtra("ID_PRODUTO", 0) Being the second parameter 0 a default value! This must be inside the onCreate

  • Thank you very much :d I was already lost

  • 1

    Here is another question that deals with how to move from one Activity to another: http://answall.com/questions/131800/android-studio-como-coletar-os-dados-de-outras-telas-e-mostrar-na-ultima-tela/131842#131842

  • what I return inside the getApplicationContext getter()

  • This is a method of your Activity! you need to pass a Context

  • I passed, now you’re making a mistake on the objectCorrente.getId()

  • Which error? how is the objectCurrent ? This object has a getId() ?

Show 4 more comments

Browser other questions tagged

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