我要做的是获取gridview中选择的项目列表。
当用户选择或取消选中我需要在数组中存储“true”或“false”的复选框时,为gridview添加了一个复选框,无论该行是否被选中。
适配器代码如下:
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
pos = position;
View MyView = convertView;
if (convertView == null) {
holder = new ViewHolder();
MyView = inflater.inflate(R.layout.hospital_row, null);
holder.tv = (TextView)MyView.findViewById(R.id.hospital_name);
holder.address = (TextView)MyView.findViewById(R.id.address);
holder.selected = (CheckBox)MyView.findViewById(R.id.checkBox1);
MyView.setTag(holder);
} else {
holder = (ViewHolder) MyView.getTag();
}
holder.address.setText(addressValues[position]);
holder.tv.setText(procedureValues[position] );
holder.selected.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.i("adapter","pos = "+ pos);
if(holder.selected.isChecked()){
arrlist.HOSPITALSELECTED[pos] = "true";
}else{
arrlist.HOSPITALSELECTED[pos] = "false";
}
for(String sel : arrlist.HOSPITALSELECTED){
Log.e("Hospital Adapter", "Selected =" + sel);
}
}
});
return MyView;
}
class ViewHolder {
TextView tv;
TextView address;
CheckBox selected;
int id;
}
Log cat:我在网格视图中有3个元素,但它只返回2& 0位置
02-03 14:46:04.605: I/adapter(816): pos = 2
02-03 14:46:04.605: E/Hospital Adapter(816): Selected =true
02-03 14:46:04.617: E/Hospital Adapter(816): Selected =false
02-03 14:46:04.625: E/Hospital Adapter(816): Selected =true
02-03 14:46:08.015: I/adapter(816): pos = 2
02-03 14:46:08.015: E/Hospital Adapter(816): Selected =true
02-03 14:46:08.025: E/Hospital Adapter(816): Selected =false
02-03 14:46:08.025: E/Hospital Adapter(816): Selected =true```