2

在下面的代码中,在 ArrayAdapter 内部有 getTag() 的 else 部分

  holder = (ViewHolder) convertView.getTag();

如何使用此部分更新或获取视图项的最新状态,并使该状态与保存的状态保持一致。就像您单击一个复选框或从数据库中获取复选框的选中状态并希望它以正确的复选框状态显示?

在我的应用程序中,我从数据库中获取复选框的先前状态,并在创建 Listview 时将复选框设置为该状态,如果框的状态发生更改,则该状态更改将保存到数据库和复选框还将显示状态的变化。

换句话说,除了 getTag 行之外,我应该在该部分代码中添加什么?

  if ((convertView == null)){

                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.smalltank_customer_row, null);

                holder = new ViewHolder();

                holder.textViewOne = (TextView) convertView.findViewById(R.id.textView1);
                holder.textViewTwo = (TextView) convertView.findViewById(R.id.textView2);
                holder.textViewThree = (TextView) convertView.findViewById(R.id.textView3);
                holder.radioGroupOne = (RadioGroup) convertView.findViewById(R.id.radioGroup1);
                holder.radioButtonOne = (RadioButton) convertView.findViewById(R.id.radioButton1);
                holder.radioButtonTwo = (RadioButton) convertView.findViewById(R.id.radioButton2);
                holder.checkBoxOne = (CheckBox) convertView.findViewById(R.id.checkBox1);
                holder.buttonOne = (Button) convertView.findViewById(R.id.button1);
                holder.buttonTwo = (Button) convertView.findViewById(R.id.button2);
                holder.buttonThree = (Button) convertView.findViewById(R.id.button3);
                holder.buttonFour = (Button) convertView.findViewById(R.id.button4);

                convertView.setTag(holder);

            }else{

                holder = (ViewHolder) convertView.getTag();

            }
4

2 回答 2

2

由于 convertView 可以回收并且通常非常不稳定,因此通过扩展它的 viewHolder 也同样不稳定,因此您不应该在那里存储任何持久性数据,例如复选框选择。您应该将您的复选框选择值存储在某个地方,当您的 Activity 暂停和恢复时,该值将保持不变。

例如,我在我的一个应用程序中使用了一个 HashSet 来保存用户选择的联系人,这些联系人在 Activity 暂停和恢复时被保存和恢复,示例代码:

private HashSet<ContactData> selSet = new HashSet<ContactData>();

public View getView(int position, View convertView, ViewGroup parent) {
    if ((convertView == null)){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.smalltank_customer_row, null);

        holder = new ViewHolder();
        //init holder
        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }
    holder.status.setChecked(selSet.contains(data));
    return convertView;
}

public void onClick(View v) {
    UserHolder holder = (UserHolder) v.getTag();
    if (selSet.contains(holder.user)) {
        selSet.remove(holder.user);
    } else {
        selSet.add(holder.user);
    }
}

public final void onCreate(Bundle savedInstanceState) {
    if (savedInstanceState != null) {
        selSet = (HashSet<ContactData>) savedInstanceState
                .getSerializable("selSet");
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putSerializable("selSet", selSet);
}
于 2013-07-31T10:22:37.280 回答
0

首先创建这个类来保存当前检查项的信息

    public class Model {

      private String name;
      private boolean selected;

      public Model(String name) {
      this.name = name;
      selected = false;
    }

     public String getName() {
      return name;
   }

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

    public boolean isSelected() {
    return selected;
  }

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

} 

然后在你的 getView()

    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;
  }
} 
于 2013-07-31T09:59:01.513 回答