0

我已经研究了这个问题的其他几个解决方案,但他们似乎都没有为我做这件事,只是因为它们都是根本不同的。我的适配器中有 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;
}
4

2 回答 2

0

尝试移动convertView.setTag(holder);到上面return convertView;

于 2013-06-13T20:12:43.230 回答
0

您可能会遇到视图回收问题。您需要明确设置 holder.category1 和 holder.category2 的状态。

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

于 2013-06-13T20:08:01.543 回答