I have a ListVew, and in this, I have two buttons (delete and cancel) my list consists of the following items: ID (TextView), Name (TextView), Nickname (TextView), and the buttons quoted above.
How do I get the ID by clicking the delete button?
I have a ListVew, and in this, I have two buttons (delete and cancel) my list consists of the following items: ID (TextView), Name (TextView), Nickname (TextView), and the buttons quoted above.
How do I get the ID by clicking the delete button?
You can implement a interface
that makes a communication between your adapter
and another class you want.
Create a interface
:
public interface MyButtonListClickListener {
public void onClickBtnExcluir(int id);
public void onClickBtnCancelar(int id);
}
In your 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 listener
is not null, so there is no exception when your interface
has not yet been implemented.
Then, in any class you use your adapter
, you can include this interface
and implement the onClickBtnExcluir
and onClickBtnCancelar
methods as you wish. The implementation of interface
would be something like: / p>
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
}
});
You should implement the click of these buttons inside your adapter:
public class ListAdapter extends ArrayAdapter<Item> {
public ListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public ListAdapter(Context context, int resource, List<Item> items) {
super(context, resource, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.itemlistrow, null);
}
final Item item = getItem(position);
TextView id= (TextView) v.findViewById(R.id.id);
TextView nome = (TextView) v.findViewById(R.id.nome);
TextView apelido = (TextView) v.findViewById(R.id.apelido);
Button btnExcluir = (Button)v.findViewById(R.id.btnExcluir);
Button btnCancelar = (Button)v.findViewById(R.id.btnCancelar);
id.setText(String.valueOf(item.getId());
nome.setText(item.getNome());
apelido.setText(item.getApelido());
btnExcluir.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
//Exclua o item
}
});
btnCancelar.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
// Cancele o item
}
});
return v;
}
After deleting or canceling an item, call the adapter.notifyDataSetChanged () function on your adapter.
This was a simple example. Search for ViewHolder in listView adapters.