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?
– Jorge B.
Enter the method code
onEntrada()
– ramaral
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....
– Sergio