Recover Data from a List View

Asked

Viewed 797 times

0

I have a Listvew, and in this, I have two buttons (delete and cancel) my list consists of the following items: ID(Textview), First Name(Textview), Last Name(Textview), and the buttons cited above.

How do I get the ID by clicking the delete button?

  • 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.

2 answers

1

You must 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 cancelling an item, call the Adapter.notifyDataSetChanged() function of your Adapter.

This was a simple example. Search on Viewholder in Adapters for listview.

1


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
     }
});

Browser other questions tagged

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