ListView with Checkboxes,滚动问题

时间:2013-06-13 19:58:46

标签: android listview checkbox

我已经针对这个问题研究了其他几种解决方案,但是他们似乎都没有为我做这件事,只因为它们都是根本不同的。我在我的适配器中有PackageInfo类,它负责列出设备上安装的应用程序。那部分有效。每个应用程序旁边都有两个复选框,可以将该应用程序排序为类别1或2.我将用户的选择存储在SharedPreference中(也有帮助,可能是对存储所有用户输入的更好方法的反馈,但这不是我主要考虑的问题) )。

我已经查看了其他一些解决方案,但我认为我在这里缺少一些基本的东西,问题是当用户滚动时,复选框被随机选中或取消选中。

public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    LayoutInflater inflater = context.getLayoutInflater();


    if (convertView == null) {
        convertView = inflater.inflate(R.layout.list_layout, null);
        holder = new ViewHolder();
        holder.apkName = (TextView) convertView.findViewById(R.id.appname);
        holder.category1 = (CheckBox) convertView.findViewById(R.id.arcade);
        holder.category1.setTag(position);
        holder.category2 = (CheckBox) convertView.findViewById(R.id.educational);
        holder.category2.setTag(position);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    PackageInfo packageInfo = (PackageInfo) getItem(position);
    Drawable appIcon = packageManager.getApplicationIcon(packageInfo.applicationInfo);
    final String appName = packageManager.getApplicationLabel(packageInfo.applicationInfo).toString();
    final String appPositionArcade = "cat1"+appName;
    final String appPositionEdu = "cat2"+appName;
    appIcon.setBounds(0, 0, 55, 55);
    holder.apkName.setCompoundDrawables(appIcon, null, null, null);
    holder.apkName.setCompoundDrawablePadding(15);
    holder.apkName.setText(appName);

    holder.category1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (holder.category1.isChecked()) {
                holder.category2.setChecked(false);
                SharedPreferences stored = context.getSharedPreferences("Shared Preferences", 0);
                SharedPreferences.Editor editor = stored.edit();

                editor.putBoolean(appPositionArcade, true);
                editor.putBoolean(appPositionEdu, false);
            } else if (!holder.category1.isChecked()) {
                SharedPreferences stored = context.getSharedPreferences("Shared Preferences", 0);
                SharedPreferences.Editor editor = stored.edit();

                editor.putBoolean(appPositionArcade, false);
            }
        }
    });

    holder.category2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if (holder.category2.isChecked()) {
                SharedPreferences stored = context.getSharedPreferences("Shared Preferences", 0);
                SharedPreferences.Editor editor = stored.edit();

                holder.category1.setChecked(false);
                editor.putBoolean(appPositionArcade, false);
                editor.putBoolean(appPositionEdu, true);
            } else if (!holder.category2.isChecked()) {
                SharedPreferences stored = context.getSharedPreferences("Shared Preferences", 0);
                SharedPreferences.Editor editor = stored.edit();

                editor.putBoolean(appPositionEdu, false);
            }
        }
    });

    return convertView;
}

2 个答案:

答案 0 :(得分:0)

您可能正在遇到查看重新打印问题。您需要显式设置holder.category1和holder.category2的状态。

因为当视图被回收时会发生什么,并且你偶然会从触发你的onclicklistener的其他数据行中留下布尔值。

答案 1 :(得分:0)

尝试将convertView.setTag(holder);移至return convertView;

之上