android - 使用 viewHolder 接口(interface)更新状态到 Listview

标签 android listview adapter android-arrayadapter

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

  holder = (ViewHolder) convertView.getTag();

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

在我的应用程序中,我从数据库中获取复选框的先前状态,并在创建 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();

            }

最佳答案

由于 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);
}

关于android - 使用 viewHolder 接口(interface)更新状态到 Listview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17964401/

相关文章:

java - 适用于 Windows 的网络适配器

java - 适配器类出现问题,没有崩溃,但页面卡住并重新加载

android - Android应用突然崩溃

java - 无需使用按钮即可自动打开 Bottom Sheet

android - 将变量从 ListView 传递到另一个 Activity

android - Android ListView 中的 Google Now 风格三点菜单

Android Spinner 使用带有 ID 的数组来处理图像资源

java - 我可以在 android xml 布局或字符串值文件中编写 java 代码吗?

android - 忽略 TortoiseSVN 中的 android 库 bin 文件?

WPF ListView 双击