You can implement a interface
that makes a communication between your adapter
and another class that wishes.
Create a interface
:
public interface MyButtonListClickListener {
public void onClickBtnExcluir(int id);
public void onClickBtnCancelar(int id);
}
In his adapter
create an attribute of this interface
and a 'set' method':
public class ListAdapter extends ArrayAdapter<Item> {
private MyButtonListClickListener listener;
.
.
.
public void setOnMyButtonListClickListener(MyButtonListClickListener listener){
this.listener = listener;
}
}
Then create the events of the clicks on the buttons in your adapter
and call the respective method of your interface
:
btnExcluir.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(listener != null){
listener.onClickBtnExcluir(item.getId());
}
}
});
btnCancelar.setOnClickListener(new OnClickListener() {
public void onClick(View v){
if(listener != null){
listener.onClickBtnCancelar(item.getId());
}
}
});
Note that there is a test if the listener
is not null, this is so that there is no exception when your interface
has not yet been implemented.
Then, in whatever class you use your adapter
, you may include this interface
and implement the methods onClickBtnExcluir
and onClickBtnCancelar
as you wish.
The implementation of interface
would be something like:
adapter.setOnMyButtonListClickListener(new MyButtonListClickListener(){
@Override
public void onClickBtnExcluir(int id) {
//Implemente como quiser e use o ID para localizar seu item no banco
}
@Override
public void onClickBtnCancelar(int id) {
//Implemente como quiser e use o ID para localizar seu item no banco
}
});
Why not put a TAG on the buttons representing the item ID? At the moment the list item on
Adapter
, just put the ID as tag.– Wakim