2015-10-14 60 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); 
        } 
相關問題