【问题标题】:How to use Shared Preferences in adapter?如何在适配器中使用共享首选项?
【发布时间】:2018-02-06 13:38:45
【问题描述】:
class SearchViewHolder extends RecyclerView.ViewHolder{

    TextView studentName, studentMatrik, studentPhone, studentAddress, studentStatus, studentMail, studentCon, studentId;
    CheckBox checkBox;
    Button buttonMap;
    DatabaseReference databaseReference;
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    boolean isChecked = sharedPreferences.getBoolean("checked", false);
    public SearchViewHolder(View itemView) {
        super(itemView);
        studentName = (TextView) itemView.findViewById(R.id.studentName);
        studentMatrik = (TextView) itemView.findViewById(R.id.studentMatrik);
        studentPhone = (TextView) itemView.findViewById(R.id.studentPhone);
        studentAddress = (TextView) itemView.findViewById(R.id.studentAddress);
        studentStatus = (TextView) itemView.findViewById(R.id.studentStatus);
        studentMail = (TextView) itemView.findViewById(R.id.studentMail);
        studentCon = (TextView) itemView.findViewById(R.id.studentCon);
        studentId = (TextView) itemView.findViewById(R.id.studentId);
        checkBox = (CheckBox) itemView.findViewById(R.id.checkBox);
        buttonMap = (Button) itemView.findViewById(R.id.buttonMap);
        buttonMap.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MapsViewActivity.class);
                intent.putExtra("Latitude",studentMail.getText().toString());
                intent.putExtra("Longitude",studentCon.getText().toString());
                intent.putExtra("Address",studentAddress.getText().toString());
                context.startActivity(intent);
            }
        });
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked) {
                    editor.putBoolean("checked", isChecked);
                    editor.commit();
                    String id = studentId.getText().toString();
                    databaseReference = FirebaseDatabase.getInstance().getReference("student");
                    String name = "Check";
                    databaseReference.child(id).child("stat").setValue(name);
                    notifyDataSetChanged();
                } else {
                    String id = studentId.getText().toString();
                    databaseReference = FirebaseDatabase.getInstance().getReference("student");
                    String name = "Not Check";
                    databaseReference.child(id).child("stat").setValue(name);
                    notifyDataSetChanged();
                }
            }
        });
    }

如何使用共享首选项将复选框值保存在适配器中?我已经尝试过,但仍然错了......如果我选中了复选框,我希望在退出并重新打开应用程序时仍然选中该复选框,并且在未选中该复选框时它会执行相同的操作。

【问题讨论】:

  • 你必须制作 hashmap 来存储复选框值和 id 以帮助添加或删除并将其保存到共享首选项中(如果需要)
  • 首先,您需要知道如何存储/读取 sharedPreference 上的值。这将有助于理解:stackoverflow.com/a/3624358/5167909

标签: android checkbox sharedpreferences


【解决方案1】:

是否设置

checkBox.setChecked(isChecked);   

不能按你的意愿工作?

【讨论】:

    【解决方案2】:

    在 Check 事件中,将布尔数据保存到 SharedPreferences,并以列表项的 id 作为键。然后将一些代码添加到ViewHolder 以检查SharedPreference 是否应在显示之前检查项目。这样,ViewHolder 可以在每次显示时检查 CheckBox 是否在 SharedPreference 中检查值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-17
      • 1970-01-01
      • 1970-01-01
      • 2019-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多