samedi 28 avril 2018

How can I save the selected values ​of a CheckBox from a user in FireBase?

How can I save the selected values ​of a CheckBox from a user with the FireBase uid and stored in SharedPreferences?

Here is my code.

public class ingredientesSemanales extends AppCompatActivity
{
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
    DatabaseReference ingredientes;
    ArrayList<String> selectedItems = new ArrayList<>();
    String[] items;
    ListView ch1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        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);
        ch1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            SharedPreferences preferences=getSharedPreferences("ItemsSelectedFromUser", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();
            @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);

                }

                editor.putString();
            }
        });
    }

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

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




Aucun commentaire:

Enregistrer un commentaire