How to know which button was clicked on my ListView?

1

I have a ListView, on each Line I have two buttons. How do 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 button methods:

CliqueBotao
CliqueBotao2

But they always have the same id, because I'm setting the id in entrada.xml , so they're always the same.

How can I differentiate them?

    
asked by anonymous 04.08.2016 / 06:53

3 answers

0

1) Create a method with View Argument.

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

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

example code: (being listView 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: You will not need to use OnItemClickListener .

    
04.08.2016 / 09:25
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
     }
 });
    
04.08.2016 / 07:45
1

Hello! You will know which button you clicked by the id you placed in the 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;
    }
    
04.08.2016 / 19:38