0

I have some issue with a Custom Adapter using a CheckBox. On click, I create an array containing some data, and this works perfectly, but in my holder there is also a 'field' that contains a number and I shoud want to put this number and eventually add it to previous selection, if user checks more rows of listview. 'holder.durata' contains that number that I should want to use.Can someone helps me to do it? Here is my Adapter

public class NuovoAdapter extends BaseAdapter {

private LayoutInflater layoutinflater;
private Context context;
ArrayList<HashMap<String, String>> dataList;
ArrayList<HashMap<String,String>> list_selected;
boolean [] itemChecked;
boolean[] checkBoxState;



public NuovoAdapter(Context context, ArrayList<HashMap<String, String>> list_serv)  {
    super();
    this.context = context;
    this.dataList = list_serv;
    itemChecked = new boolean [dataList.size()];
    list_selected = new ArrayList<HashMap<String, String>>();
    checkBoxState=new boolean[dataList.size()];
    list_selected.clear();


}
@Override
public int getCount() {
    return dataList.size();
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    if (convertView == null) {

        convertView = inflater.inflate(R.layout.prenota_serv, null);
        holder = new ViewHolder();

        holder.id = (TextView) convertView.findViewById(R.id.et_id);
        holder.service = (TextView) convertView.findViewById(R.id.et_serv);
        holder.costo = (TextView) convertView.findViewById(R.id.et_costo);
        holder.durata = (TextView) convertView.findViewById(R.id.et_dur);
        holder.Ceck = (CheckBox) convertView.findViewById(R.id.checkBox);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }


    holder.id.setText(dataList.get(position).get("ID"));
    holder.service.setText(dataList.get(position).get("Descrizione"));
    holder.costo.setText(dataList.get(position).get("Costo"));
    holder.durata.setText(dataList.get(position).get("Durata_m"));
    //holder.Ceck.setChecked(false);
    holder.Ceck.setChecked(checkBoxState[position]);


    holder.Ceck.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            if(((CheckBox)v).isChecked())
                checkBoxState[position]=true;
            else
                checkBoxState[position]=false;

        }
    });




     holder.Ceck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                                     boolean isChecked) {
            HashMap<String, String> data = dataList.get(position);
            if (isChecked) {
                addToSelected(data);
            } else {
                removeSelected(data);
            }
        }

   });

    return convertView;

}
private void addToSelected(HashMap contact){
    if(!list_selected.contains(contact))list_selected.add(contact);

}
private boolean removeSelected(HashMap contact){
    return list_selected.remove(contact);
}
public ArrayList<HashMap<String, String>> getSelectedItems(){
    return list_selected;
}

}

class ViewHolder {
TextView id;
TextView service;
TextView costo;
TextView durata;
CheckBox Ceck;
 }
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Jayelef
  • 135
  • 12
  • 1
    this [link](http://stackoverflow.com/questions/10911361/how-to-get-selected-list-items-from-a-listview-with-checkbox-and-custom-adapter) may helps you – pRaNaY Oct 14 '15 at 17:34
  • @pRaNaY Thanks, I think so, but I have to study it before!! I will try to adapt that example. – Jayelef Oct 14 '15 at 17:46
  • @pRaNaY maybe it looks like what I want to do, but I don't be able to applicate this example to solve my issue. I already have an array (it's list_selected): Can I sum the elements called 'Durata_m'? There is a way to do this? – Jayelef Oct 15 '15 at 11:18

1 Answers1

0

SOLVED

I don't know if it's correct, but it works properly and I hope to help someone with the same issue. Using my CustomAdapter, I already have an array that stores data everytime user select the Checkbox. In my activity, i have converted to 'Int value', the only key 'Durata_m' and I have added it using a cycle. If someone can do better, it would be apreciated.Here is the code.

            Button bSend = (Button) findViewById(R.id.btnSend);
        bSend.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                ArrayList<HashMap<String,String>> list_selected = adapter.getSelectedItems();
                int HourVal = 0;

                for (HashMap<String, String> map : list_selected)
                    for (Map.Entry<String, String> mapEntry : map.entrySet())
                    {

                        switch (mapEntry.getKey()){
                            case("Durata_m"):
                                HourVal = HourVal + Integer.parseInt(mapEntry.getValue().toString());

                        }
                        System.out.println("TOTAL MINUTES: " + HourVal);
                    }
Jayelef
  • 135
  • 12