【问题标题】:how can get chipgroup values when the user selected and submit to firebase?当用户选择并提交到firebase时如何获取chipgroup值?
【发布时间】:2021-07-03 02:32:36
【问题描述】:

当用户选择并提交到firebase时如何获取chipgroup值?

我已经编写了代码,但是当用户选择并将日期提交到 firebase 时,我只得到了值 -1。我不知道发生了什么。

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

我希望我能得到像 wifi、campsite_wheelchair、campsite_suit_any_car 等这样的价值

【问题讨论】:

    标签: android android-chips


    【解决方案1】:

    我不知道这是否是一个好方法,但您可以在 setOnCheckedChangeListener 中获取值;

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

    你也可以使用 switch。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-21
      • 2013-02-16
      • 2017-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-27
      相关资源
      最近更新 更多