How to know which Boot was clicked on my Listview?

Asked

Viewed 1,476 times

1

I have a Listview, in each row I have two buttons. How can I know which button was clicked?

Follow my code:

<ImageView
    android:id="@+id/imageView_imagen"
    android:layout_width="145dp"
    android:layout_height="145dp"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    android:contentDescription="Descripción del contenido de la imagen"
    android:src="@android:drawable/ic_menu_gallery" />

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView_superior"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_marginLeft="25dp" />

    <Button
        android:id="@+id/bt_botao"
        android:layout_width="wrap_content"
        android:onClick="CliqueBotao"
        android:layout_height="wrap_content"

        android:layout_gravity="right"

        android:layout_weight="0"
        />

    <TextView
        android:id="@+id/textView_inferior"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_marginLeft="25dp" />


    <Button
        android:id="@+id/bt_botao2"
        android:layout_width="wrap_content"
        android:onClick="CliqueBotao2"
        android:layout_height="wrap_content"

        android:layout_gravity="right"

        android:layout_weight="1"
        />

</LinearLayout>

My Adapter:

public abstract class Lista_adaptador extends BaseAdapter {

    private ArrayList<?> entradas;
    private int R_layout_IdView;
    private Context contexto;

    public Lista_adaptador(Context contexto, int R_layout_IdView, ArrayList<?> entradas) {
        super();
        this.contexto = contexto;
        this.entradas = entradas;
        this.R_layout_IdView = R_layout_IdView;
    }

    @Override
    public View getView(int posicion, View view, ViewGroup pariente) {
        if (view == null) {
            LayoutInflater vi = (LayoutInflater) contexto.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R_layout_IdView, null);
        }
        onEntrada(entradas.get(posicion), view);
        return view;
    }

    @Override
    public int getCount() {
        return entradas.size();
    }

    @Override
    public Object getItem(int posicion) {
        return entradas.get(posicion);
    }

    @Override
    public long getItemId(int posicion) {
        return posicion;
    }

    /**
     * Devuelve cada una de las entradas con cada una de las vistas a la que debe de ser asociada
     *
     * @param entrada La entrada que será la asociada a la view. La entrada es del tipo del paquete/handler
     * @param view    View particular que contendrá los datos del paquete/handler
     */
    public abstract void onEntrada(Object entrada, View view);

}

And in my Acticitymain, I have the two methods of buttons:

CliqueBotao
CliqueBotao2

But they always have the same id because I’m setting the id on entrada.xml, then they’re always the same.

How can I tell them apart?

  • Do you want to know which of the two was clicked on 1 or 2? or which line was clicked on?

  • Enter the method code onEntrada()

  • Each line is from a user, and contains two buttons...one of message is another that calls an Activity.... In my activityMain I always get them from my XML...to make the lines...but at that moment I wanted to set a different id, for when it is clicked I know: this button is from message of such user... In my actMaim I have already implemented the two methods that are working.... But when I get the id by view.id()... Always comes the same id of one and the same id of the other....

3 answers

1

Hello! You will know which button clicked by the id you put in xml. The only thing you have to worry about is putting the event inside the getView of your Adapter.

    @Override
    public View getView(int posicion, View view, ViewGroup pariente) {

        // Birl

        if (view == null) {
            LayoutInflater vi = (LayoutInflater) contexto.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R_layout_IdView, null);
        }
        onEntrada(entradas.get(posicion), view);

        Button bt_botao = (Button) view.findViewById(R.id.bt_botao);
        Button bt_botao2 = (Button) view.findViewById(R.id.bt_botao2);

        bt_botao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Acao do primeiro botao
            }
        });

        bt_botao2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Acao do segundo botao
            }
        });


        return view;
    }

1

You can enter in onCreate:

 Button bt_botao2 = (Button) findViewById(R.id.bt_botao2 );
 bt_botao2.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         //codigo a ser executado aqui
     }
 });

0


1) create a method with View Argument.

2) use switch (or if) using view.getId() , this method returns the int id of the button that received the event (the click).

3) add the attribute android:onClick="nomeDoMethod"

example code: (listview being the object of your Listview)

 public void buttonClicked(View v)
 {
     int pos = listView.getPositionForView(view);

     switch(v.getId())
     {
         case R.id.bt_botao :
             Log.d("TAG","BUTTON 1 @ position: "+pos);
             break;
         case R.id.bt_botao2 :
             Log.d("TAG","BUTTON 2 @ position: "+pos);
             break:
         default :
         // caso aconteca algum erro 
         break;
     }
 }

now within the layout.xml where you find the Buttons:

 <Button
    android:id="@+id/bt_botao2"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:layout_weight="1"
    android:onClick="buttonClicked"
    />

PS: it will not be necessary to use the OnItemClickListener.

  • Adriano...I’m already doing this...and the two methods are working when I put in the buttons he enters the method...my problem is in distinguishing the buttons......

  • and so you have to use the int pos = listView.getPositionForView(view); this method will return you the position of your Listview where the Button received the event (click) , ai vc use as index of your Array that you passed to your Adapter, by q understood the layout.xml where you find your Buttons and the layout of the item of your Listview , then you can only find them by id + position

  • I saw your comment , you then have an array with ID’s , makes a Log. d() inside the case : and uses 'seuArrayId[pos]' the pos would be the index , ai vc can get the id you want

  • Okay, Adliano...worked out the way you explained at first... thank you.

Browser other questions tagged

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