2015-10-14 71 views
0

我对使用复选框的自定义适配器有一些问题。点击后,我创建一个包含一些数据的数组,并且这个工作完美,但是在我的持有者中还有一个包含数字的“字段”,我希望将该数字添加到以前的选择中,如果用户检查更多行的列表视图。 'holder.durata'包含我应该使用的那个数字。可以有人帮我做到这一点吗? 这里是我的适配器自定义适配器,复选框上的总和值点击

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

此[链接](http://stackoverflow.com/questions/10911361/how-to-get-selected-list-items-from-a-listview-with-checkbox-and-custom-adapter )可以帮助你 – pRaNaY

+0

@pRaNaY谢谢,我想是的,但我必须先研究它!我会尝试去适应这个例子。 – Jayelef

+0

@pRaNaY也许它看起来像我想要做的,但我无法应用此示例来解决我的问题。我已经有一个数组(它是list_selected):我可以将名为'Durata_m'的元素求和吗?有一种方法可以做到这一点? – Jayelef

回答

0

解决

我不知道这是否是正确的,但它工作正常,我希望能帮助别人用同样的问题。使用我的CustomAdapter,我已经有了一个数组,每次用户选择复选框时都会存储数据。在我的活动中,我已转换为'Int value',唯一的关键'Durata_m',并且我使用循环添加了它。如果有人可以做得更好,这将是一个好消息。这里是代码。

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