mercredi 8 juin 2022

Android spinner not select anything at launch

I basically want the spinner to appear when the user checks the checkbox (it works). And the spinner has items for the user to pick but I know that spinner selects the first item automatically when launched.

-- Issue -- : whenever I check the checkbox and spinner appears, I can press the first item BUT the spinner does not GO AWAY and I can't get the String text of the selected item (as I use it to set the text of the CheckBox). I can only selected the items after the first one and then, WHEN: I check and uncheck the box I can only select other options and not the same one when selected before.

-- Goal -- : to be able to select the first item when checkbox appears if user selects it, and set the String text onto the Checkbox AND be able to select the same item even after the user unchecks and checks the checkbox.

Code for both checkbox and spinner:

 //checkbox
        giftwrapchecker = findViewById(R.id.checker);
        notEligible();
        giftwrapchecker.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b==false){giftwrapoptions.setVisibility(View.GONE); pricetagTv.setText("RM " + (bottles * perbottle)); giftwrapchecker.setText(R.string.giftq);}
                else {giftwrapoptions.setVisibility(View.VISIBLE);setGiftprice();}
            }
        });

        //spinner to check if the user has interacted with the spinner yet
        giftwrapoptions.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                isTouched = true;
                return false;
            }
        });


        //getting user selection and removes spinner once user has chosen and also set the chosen item's text onto the checkbox

        giftwrapoptions.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            //this is the first item that the spinner auto selects
            String preselecteditem = giftwrapoptions.getSelectedItem().toString();
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                if (isTouched) {
                    //this is the item that user has selected
                    String item =  giftwrapoptions.getSelectedItem().toString();
                    System.out.println("Selected "+item);
                    giftwrapchecker.setText(item);
                    giftwrapoptions.setVisibility(View.GONE);

                    if (item==preselecteditem){
                        giftwrapchecker.setText(preselecteditem);
                        giftwrapoptions.setVisibility(View.GONE);}

                    }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {}
        });

    }

    private int getSum() {
        int sum = 0;
        for (int i=0;i< qty.size();i++){
            sum += (Integer.parseInt(qty.get(i)));
        }
        return sum;
    }
  
    private void notEligible(){
        if (bottles==3 || bottles==6 || bottles==12){
            giftwrapchecker.setEnabled(true);
            giftwrapoptions.setEnabled(true);
           
        } else{
            giftwrapchecker.setChecked(false);
            giftwrapchecker.setEnabled(false);
            giftwrapchecker.setText(R.string.giftna);
            giftwrapoptions.setVisibility(View.GONE);
            giftwrapoptions.setEnabled(false);
        }
        pricetagTv.setText("RM " + (bottles * perbottle));
    }



Aucun commentaire:

Enregistrer un commentaire