Cheackbox on a Basedapter, how to use it?

Asked

Viewed 96 times

0

Hello, I’m developing my TCC, and I don’t know how to use checkbox inside a baseAdapter. My idea is, I have a window that shows me a list of orders and at the bottom of the list this total, each item on that list has a checkbox, and I want to make sure that when I click on a checkbox I bring the price of that item and if I select more than one, add the values, however I have no idea how to do this, I searched the internet, but found nothing.

Here is my baseAdapater:

public class FinalizarAdapter extends BaseAdapter {

private List<Auxiliar> pedido;
@SuppressWarnings("unused")
private Context context;
private LayoutInflater inflater;

public FinalizarAdapter(Context context, List<Auxiliar> pedido) {
    this.pedido = pedido;
    this.context = context;
    this.inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return pedido.size();
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return pedido.get(arg0);
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return arg0;
}

@Override
public View getView(int position, View view, ViewGroup parent) {
    ViewHolder holder = null;

    if (view == null) {
        holder = new ViewHolder();
        int layout = R.layout.layout_inflater_finalizar_pedido;
        view = inflater.inflate(layout, null);
        view.setTag(holder);

        holder.tvNome = (TextView) view.findViewById(R.id.tvLvItem);
        holder.tvQtd = (TextView) view.findViewById(R.id.tvLvQtd);
        holder.cbTotal = (CheckBox) view.findViewById(R.id.cbTotal);

    } else {

        holder = (ViewHolder) view.getTag();
    }

    Auxiliar pedi = pedido.get(position);

    holder.tvNome.setText("" + pedi.getNome());
    holder.tvQtd.setText("" + pedi.getQtd());       

    return view;
}   

public class ViewHolder {
    CheckBox cbTotal;
    TextView tvNome;
    TextView tvQtd;
}

}

1 answer

0

You have to create declare a Onclicklistener and associate it with Checkbox of view of each of the rows on your list.
Within the method onClick put the code you want to execute whenever the Checkbox is clicked.

if (view == null) {
    holder = new ViewHolder();
    int layout = R.layout.layout_inflater_finalizar_pedido;
    view = inflater.inflate(layout, null);
    view.setTag(holder);

    holder.tvNome = (TextView) view.findViewById(R.id.tvLvItem);
    holder.tvQtd = (TextView) view.findViewById(R.id.tvLvQtd);
    holder.cbTotal = (CheckBox) view.findViewById(R.id.cbTotal);

    holder.cbTotal.setOnClickListener( new View.OnClickListener() {  
        public void onClick(View v) {  
            CheckBox cb = (CheckBox) v;  

            //Código a ser executado quando o checbox é clicado.
        }  
    });  


} else {

    holder = (ViewHolder) view.getTag();
}

Note: Depending on how you manage your list it may be necessary for the class Auxiliary must have an attribute that preserves the state of the Checkbox.

Behold here an example

Browser other questions tagged

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