android - 从具有共享首选项的列表中保存复选框状态

标签 android android-listview android-studio sharedpreferences android-checkbox

我问了一个关于如何使用文本文件保存复选框状态的问题,但建议使用共享首选项。我在使用文本文件之前尝试过这种方法,但遇到了同样的问题 - 列表中的复选框看似随机地选中和取消选中。

我的应用程序显示设备上当前安装的应用程序的列表,它显示应用程序的标题、图标和复选框。如果我选中其中一项并再次向下滚动并返回,它通常会取消选中,而其他项目会被随机选中。我试图获取它,以便从共享首选项加载复选框状态,并在用户手动选中该框时保存。

这是 ListView 自定义适配器的代码:

    public class AppDetailsAdapter extends BaseAdapter {

private List<AppDetails> data;
private Context context;


public AppDetailsAdapter(Context context, List<AppDetails> data) {
    this.context = context;
    this.data = data;
}


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

    if (view == null) {
        LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = vi.inflate(R.layout.custom_row_layout, null);
    }

    ImageView icon = (ImageView) view.findViewById(R.id.icon);
    TextView text = (TextView) view.findViewById(R.id.text);
    final CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox);
    final AppDetails item = data.get(position);

    text.setText(item.name);
    icon.setImageDrawable(item.icon);

    SharedPreferences settings = context.getSharedPreferences("data",Context.MODE_PRIVATE);
    boolean Checked = settings.getBoolean(item.name, false);
    checkBox.setChecked(Checked);

    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // Handle your conditions here
        if(checkBox.isChecked()==true){

            SharedPreferences settings = context.getSharedPreferences("data", Context.MODE_PRIVATE);
            settings.edit().putBoolean(item.name,true).commit();
            Toast.makeText(context,"You selected "+item.name, Toast.LENGTH_SHORT).show();
        }
        else {
            SharedPreferences settings = context.getSharedPreferences("data", Context.MODE_PRIVATE);
            settings.edit().putBoolean(item.name,false).commit();
            Toast.makeText(context,"You deselected "+item.name, Toast.LENGTH_SHORT).show();
        }




    }
    });


    return view;
}

}

最佳答案

发生这种情况是因为 View 回收。

你能尝试一件事吗:
而不是这个

   if (view == null) {
        LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = vi.inflate(R.layout.custom_row_layout, null);
    }

直接写

  LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = vi.inflate(R.layout.custom_row_layout, null);

再次运行。

关于android - 从具有共享首选项的列表中保存复选框状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29395371/

相关文章:

java - Android Studio Box2D 循环工作时出现问题

java - 创建新项目时android studio出错

java - Android FragmentManager BackStackRecord.run 抛出 NullPointerException

android - 缓存从互联网上获取的图像?

android - 在 Listview 中模拟用户对 EditText 的触摸操作

android - 具有动态加载图像的 ListView - 如何等待 ListView 加载所有图像,然后显示 ListView

android - 拆分安装 API 不可用。安装动态功能 Android Studio 时出现错误代码 - 5

android - 为 Lollipop 之前的可绘制选择器设置主题

android - 如何选择最佳图像大小以不超过 VM 预算?

android - 在 subview 上使用 onclicklistener 获取 JSON 对象