簡體   English   中英

如何在適配器中使用共享首選項?

[英]How to use Shared Preferences in adapter?

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();
                }
            }
        });
    }

如何使用共享首選項將復選框值保存在適配器中? 我嘗試這樣做,但仍然出錯...如果我已選中復選框,則我希望在退出並重新打開應用程序時仍選中該復選框,並且在取消選中該復選框時也將執行相同的操作。

是否設置

checkBox.setChecked(isChecked);   

不能讓它按需工作嗎?

在Check事件中,將布爾數據保存到SharedPreferences 中,列表項ID為key 然后將一些代碼添加到ViewHolder以在顯示項目之前檢查是否應檢查項目的SharedPreference 這樣, ViewHolder可以在每次顯示時檢查CheckBoxSharedPreference是否具有選中值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM