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;
}
此[鏈接](http://stackoverflow.com/questions/10911361/how-to-get-selected-list-items-from-a-listview-with-checkbox-and-custom-adapter )可以幫助你 – pRaNaY
@pRaNaY謝謝,我想是的,但我必須先研究它!我會嘗試去適應這個例子。 – Jayelef
@pRaNaY也許它看起來像我想要做的,但我無法應用此示例來解決我的問題。我已經有一個數組(它是list_selected):我可以將名爲'Durata_m'的元素求和嗎?有一種方法可以做到這一點? – Jayelef