行onClick影响其他行

时间:2013-03-28 10:56:37

标签: java android listview view

我正在创建一个检查清单,用于标记“学生”是否存在。 我有一个listView创建成功的详细信息和右侧;一个ImageView“X”。点击该行时,“X”变为勾号!它成功地改变了我使该程序工作所需的其他事情。

我的问题“是”,当我将其中一个“勾选”的行滚出视图时,它将恢复为“X”。我使用View Holder解决了这个问题。

我目前的问题是,例如,在6名学生名单中。如果我“勾选”学生1,学生5也将被改为勾选(尽管不在视线范围内)。如果我勾选学生2,学生6将被勾选。这反过来也是如此。我知道onClick代码没有为“随机更改的行”执行,因为尽管被勾选,这些行也没有被标记为“勾选”。这对我来说很奇怪,这是我的代码:

ListView listView = (ListView) findViewById(R.id.student_listview);
    listView.setBackgroundColor(Color.TRANSPARENT); 
    listView.setCacheColorHint(Color.TRANSPARENT);
    listView.setAdapter(new MySimpleArrayAdapter(this, studentIDarr)); 
    listView.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

            ImageView img = (ImageView) view.findViewById(R.id.icon);

            if(img.getTag().toString().equals("checked")) {
                checkListArr.set(position, "Absent");
                img.setImageResource(R.drawable.remove);
                img.setTag("unchecked");
            } else {
                checkListArr.set(position, "Present");
                img.setImageResource(R.drawable.checked);
                img.setTag("checked");
            }  
          }
        }); 

使用ViewHolder的适配器......

static class ViewHolder {
      TextView fname;
      TextView sname;
      TextView ID;
      TextView email;
      ImageView status;
    }

public class MySimpleArrayAdapter extends ArrayAdapter<String> {
      private final Context context;
      private final ArrayList<String> values;

      public MySimpleArrayAdapter(Context context, ArrayList<String> values) {
        super(context, R.layout.list_class_item, values);
        this.context = context;
        this.values = values;
      }

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {

          LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

          View vi = convertView;
          ViewHolder holder;
          if (convertView == null) {
                vi = inflater.inflate(R.layout.list_student_item, null);
                holder = new ViewHolder();
                holder.fname = (TextView) vi.findViewById(R.id.tvFname);
                holder.sname = (TextView) vi.findViewById(R.id.tvSname);
                holder.ID = (TextView) vi.findViewById(R.id.tvStudentID);
                holder.email = (TextView) vi.findViewById(R.id.tvEmail);
                holder.status= (ImageView) vi.findViewById(R.id.icon);

                if(checkListArr.get(position).toString().equals("Absent")) {
                    holder.status.setTag("unchecked");
                } else if (checkListArr.get(position).toString().equals("Present")) {
                    holder.status.setTag("checked");
                }       

                vi.setTag(holder);
            } else {
                holder = (ViewHolder) vi.getTag();
            }    

        if(checkListArr.get(position).toString().equals("Absent")) {
            holder.status.setTag("unchecked");
        } else if (checkListArr.get(position).toString().equals("Present")) {
            holder.status.setTag("checked");
        }           

        holder.ID.setText(values.get(position));
        holder.fname.setText(fNameArray.get(position));
        holder.sname.setText(sNameArray.get(position));
        holder.email.setText(emailArray.get(position));
        return vi;
      }
    } 

有人请指出我的代码中明显的缺陷!谢谢你的阅读!

2 个答案:

答案 0 :(得分:1)

您误解了ListView如何回收以前的视图。您需要将这些更改存储在基础数据模型中。

public class Model {

  private String fname;
  private String sname;
  private String email;
  private boolean selected;

  public Model(String fname, String sname, String email) {
    this.fname = fname;
    selected = false;
    //etc
  }

  public String getName() {
    return fname;
  }

  public void setName(String fname) {
    this.fname = fname;
  }

  public boolean isSelected() {
    return selected;
  }

  public void setSelected(boolean selected) {
    this.selected = selected;
  }
  //etc
} 

适配器:

private final List<Model> list;
    private final Activity context;

    public InteractiveArrayAdapter(Activity context, List<Model> list) {
        super(context, R.layout.yourlayout, list);
        this.context = context;
        this.list = list;
    }

    static class ViewHolder {
        protected TextView text;
        protected CheckBox checkbox;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.rowbuttonlayout, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) view.findViewById(R.id.label);
            viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
            viewHolder.checkbox
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                boolean isChecked) {
                            Model element = (Model) viewHolder.checkbox
                                    .getTag();
                            element.setSelected(buttonView.isChecked());

                        }
                    });
            view.setTag(viewHolder);
            viewHolder.checkbox.setTag(list.get(position));
        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(list.get(position).getName());
        holder.checkbox.setChecked(list.get(position).isSelected());
        return view;
    }

答案 1 :(得分:0)

无需ViewHolder

convertView已经是虚增的观点。当它的值为null时膨胀。然后找到您的视图并根据new或convertView中的位置设置正确的值。

使用切换SparseBooleanArray值的方法在适配器中实现<int Position, boolean Absence>。它优化了HashMap,就像保存和插入这些值一样。 AdapterView<?>将为您提供与其互动的适配器。

ListView还允许您访问包含所有选定视图的类似数组,以便进行处理和保存。