-1

how can get chipgroup values when the user selected and submit to firebase?

I already written the code, but when user selected and submit the date to firebase, I just got the value -1. I don't know what's happen.

String[] CampSite = new String[]{getApplicationContext().getResources().getString(R.string.campsite_wifi),
                getApplicationContext().getResources().getString(R.string.campsite_wheelchair),
                getApplicationContext().getResources().getString(R.string.campsite_water_from_tap),
                getApplicationContext().getResources().getString(R.string.campsite_toilet),
                getApplicationContext().getResources().getString(R.string.campsite_swimming_pool),
                getApplicationContext().getResources().getString(R.string.campsite_surface),
                getApplicationContext().getResources().getString(R.string.campsite_suit_any_car),
                getApplicationContext().getResources().getString(R.string.campsite_pet_welcome),
                getApplicationContext().getResources().getString(R.string.campsite_laundromat),
                getApplicationContext().getResources().getString(R.string.campsite_large_vehicle_access),
                getApplicationContext().getResources().getString(R.string.campsite_kitchen),
                getApplicationContext().getResources().getString(R.string.campsite_internet),
                getApplicationContext().getResources().getString(R.string.campsite_household_power),
                getApplicationContext().getResources().getString(R.string.campsite_hot_shower),
                getApplicationContext().getResources().getString(R.string.campsite_dump_station),
                getApplicationContext().getResources().getString(R.string.campsite_credit_card),
                getApplicationContext().getResources().getString(R.string.campsite_cellular_signal),
                getApplicationContext().getResources().getString(R.string.campsite_caravan_power),
                getApplicationContext().getResources().getString(R.string.campsite_cabin),
                getApplicationContext().getResources().getString(R.string.campsite_bed_supplied),
                getApplicationContext().getResources().getString(R.string.campsite_bbq),
        };
        chipGroup = findViewById(R.id.CampSite);

        for (String camp : CampSite)
        {
            Chip chip = new Chip(this);
            chip.setText(camp);
            chip.setChipBackgroundColorResource(R.color.colorAccent);
            chip.setCloseIconVisible(true);
            chip.setTextColor(getResources().getColor(R.color.white));
            //chip.setTextAppearance(R.style.ChipTextAppearance);
            chip.setId(ViewCompat.generateViewId());

            // edit:
            chip.setCheckable(true);
            chipGroup.addView(chip);
        };
        // OnclickChange item to check
        chipGroup.setOnCheckedChangeListener(new ChipGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(ChipGroup group, int checkedId) {
                group.isClickable();
            }
        });

 DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Campsite");
 String postid = reference.push().getKey();

hashMap.put("CamperSiteSummary", chipGroup.getCheckedChipId());

I hope I can get the value like wifi, campsite_wheelchair,campsite_suit_any_car etc

James
  • 13
  • 4

1 Answers1

1

I don't know this is a good approach or not but you can get the value in setOnCheckedChangeListener as;

String selectedKey = "";
    chipGroup.setOnCheckedChangeListener(new ChipGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(ChipGroup group, int checkedId) {
                if (checkedId == chip4.getId()) {
              selectedKey =   chip4.getText();
                } else if (checkedId == chip5.getId()) {
                  selectedKey = chip5.getText();
                } else if (checkedId == chip6.getId()) {
                  selectedKey =  chip6.getText();
                } else {
                  selectedKey =  chip7.getText();
                }
            });

You can use switch as well.

Muaz
  • 71
  • 5