CheackBox on a BaseAdapter, how to use it?

0

Hello, I'm developing my CBT, and I do not know how to use a checkbox inside a BaseAdapter. My idea is, I have a window that shows me a list of requests and at the end of the list is the total, every item in that list has a checkbox, and I want to make when I click on a checkbox I bring the price of that item and I select more than one, add values, but I have no idea how to do this, I searched the internet, but I did not find anything.

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

}
    
asked by anonymous 23.05.2015 / 21:51

1 answer

0

You have to create an OnClickListener and associate it with CheckBox from view of each of the lines in your list. > Within the onClick method, put the code that you want to run each time 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();
}
Depending on how you manage your list, you may need the Auxiliary class to have an attribute that stores the state of CheckBox / em>.

See here an example

    
24.05.2015 / 12:16