This is my code, I try everything.
这是我的代码,我尝试了一切。
I need the user when he passes to another Activity and then return to the CheckBox, the CheckBox is marked with the values previously marked, it is like a shopping list. Imake LOG to itemsString but its empty , what im missing ?, what im doing bad?
我需要用户当他传递给另一个Activity然后返回CheckBox时,CheckBox被标记为先前标记的值,它就像一个购物清单。 Imake LOG to itemsString但是它是空的,什么是im缺失?,我做什么坏事?
the problem I have is that it does not give an error
我遇到的问题是它没有出错
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 个解决方案
#1
0
First save the state of checkbox in Shared preferences like this :
首先在共享首选项中保存复选框的状态,如下所示:
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();
Then retrieve the data in onCreate
of the same activity
然后检索相同活动的onCreate中的数据
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String restoredText = prefs.getString("state", null);
here you will check if this string is equal to true you will changed the state of checkbox to checked..
在这里你将检查这个字符串是否等于true你将把复选框的状态改为检查..
#1
0
First save the state of checkbox in Shared preferences like this :
首先在共享首选项中保存复选框的状态,如下所示:
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();
Then retrieve the data in onCreate
of the same activity
然后检索相同活动的onCreate中的数据
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String restoredText = prefs.getString("state", null);
here you will check if this string is equal to true you will changed the state of checkbox to checked..
在这里你将检查这个字符串是否等于true你将把复选框的状态改为检查..