我有下面的代码,现在我需要跟踪每个gridview项目中的复选框状态,并在按钮上获取该信息以更新信息。我在imageadapter的调用活动中的按钮事件为gridview。
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
ImageView imgView = null;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater ltInflate = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
convertView = ltInflate.inflate(R.layout.griditem, null);
holder.textview1 = (TextView) convertView.findViewById(R.id.grid_item_alert_date);
holder.textview2 = (TextView) convertView.findViewById(R.id.grid_item_alert_time);
holder.textview3 = (TextView) convertView.findViewById(R.id.grid_item_alert_type);
holder.imageview = (ImageView) convertView.findViewById(R.id.grid_item_image);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.checkbox_ack);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
//Accessing or saving position to a List doesn't work here
//How to add ? info to a list here...
}
} });
holder.textview1.setText("Text 1 ");
holder.textview2.setText("Text 2 ");
holder.textview3.setText("Text 3 ");
holder.checkbox.setChecked(false);
holder.imageview.setImageBitmap(bitmap);
holder.id = position;
return convertView;
}
在活动中:
private OnClickListener UpdateButtonListener =
new OnClickListener(){
public void onClick(View v)
{
//CheckBox ckbocx = (CheckBox) findViewById(R.id.checkbox_ck);
//Need info on all the checkboxes for each gridview item
};
任何线索,提示都非常受欢迎。
答案 0 :(得分:4)
处理整数数组以在选中/取消选中时存储复选框的状态, 最初用0值填充数组,这表示未选中这样的复选框。
int[] checkStates;
checkStates = new int[datalist.length()];
for (int i = 0; i < datalist.length(); i++) {
checkStates[i] = 0;
}
现在处理复选框点击事件以获得完美的位置。当选中框时,使用settag & get gettag和内部点击事件将特定位置的值从0更改为1.
喜欢这个#
checkbox.setTag(position);
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
pos = (Integer) buttonView.getTag();
// checkStates[pos] = 1;
// pos = (Integer) v.getTag();
if (!buttonView.isChecked()) {
boxState[pos] = 0;
} else {
boxState[pos] = 1;
}
notifyDataSetChanged();
}
});
并且内部getview方法以这种方式处理你的检查/取消检查状态..
if (checkStates[position] == 0) {
checkbox.setChecked(false); // set unchecked"
} else {
checkbox.setChecked(true); // set checked"
}
通过这种方式,您将获得所选复选框的信息,进一步处理您的按钮单击事件,并获取在检查和取消选中时填充的int数组。