为什么我无法使用SharedPreferences从我的CheckBox保存状态?

时间:2018-05-02 23:38:31

标签: android android-studio arraylist checkbox sharedpreferences

这是我的代码,我尝试了一切。

我需要用户当他传递给另一个Activity然后返回CheckBox时,CheckBox被标记为先前标记的值,它就像一个购物清单。 Imake LOG to itemsString但是它是空的,我什么都不见了?,我在做什么坏事?

我遇到的问题是它没有出错

public class ingredientesSemanales extends AppCompatActivity {
    //OBTENER REFERENCIA A LA RAIZ
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
    DatabaseReference ingredientes;
    ArrayList<String> selectedItems = new ArrayList<>();
    String[] items;
    ListView ch1;
    SharedPreferences preferences;
    String itemsString;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        preferences = getSharedPreferences("ItemsSelectedFromUser", Context.MODE_PRIVATE);
        setContentView(R.layout.activity_ingredientes_semanales);
        ch1 = (ListView) findViewById(R.id.checkeable_list);
        ingredientes = ref.child("Ingredientes Semanales").child("Lunes");

        ch1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        if (preferences != null) {

            if (itemsString != null) {

                String valueFromPreferences = preferences.getString(FirebaseAuth.getInstance().getCurrentUser().getUid(), null);
                Log.d("values", valueFromPreferences.toString());
                Gson gson = new Gson();
                ArrayList values = gson.fromJson(valueFromPreferences, ArrayList.class);

                for (int i = 0; i < values.size(); i++) {
                    Log.d("values", values.toString());
                    if (selectedItems.contains(values.get(i).toString())) {
                        Log.d("selectedItems", values.get(i).toString());

                        ch1.setItemChecked(i, true);
                    }
                }

            }

        }


        /*ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.semanarow, R.id.txt_lan, items);
        ch1.setAdapter(adapter);*/

        ch1.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String selectedItem = ((TextView) view).getText().toString();
                if (selectedItems.contains(selectedItem)) {
                    selectedItems.remove(selectedItem);
                } else {
                    selectedItems.add(selectedItem);

                }

                SharedPreferences.Editor editor = preferences.edit();
                Gson gson = new Gson();
                itemsString = gson.toJson(selectedItems);
                Log.d("valueFromPreferences", itemsString.toString());
                editor.putString(FirebaseAuth.getInstance().getCurrentUser().getUid(), itemsString);
                Log.d("user", FirebaseAuth.getInstance().getCurrentUser().getUid());
                editor.apply();


            }
        });

        //  Log.d("PEPITO", ingredientes.toString());

    }

    @Override
    protected void onStart() {
        super.onStart();

        //Seters Lunes
        ingredientes.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                String value = dataSnapshot.getValue(String.class);
                items = value.split(",");
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.semanarow, R.id.txt_lan, items);
                ch1.setAdapter(adapter);

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

首先在共享偏好设置中保存复选框的状态,如下所示:

SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME,MODE_PRIVATE).edit();
//if it is checked set it true or false
editor.putString("state", "true");
editor.apply();

然后检索同一活动的onCreate中的数据

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String restoredText = prefs.getString("state", null);

在这里你将检查这个字符串是否等于true你将把复选框的状态改为检查..