dimanche 3 mars 2019

How to make item selector on spinner work

String [] product = new String [] {
            "Item A - 15$",
            "Item B - 12$",
            "item C - 30$",
            "Item D - 40$",

    };

            ArrayAdapter<String> adapter = new
            ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,product);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            final Spinner spin = (Spinner)findViewById(R.id.spinner1);
            spin.setAdapter(adapter);

            spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
                public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l){
                    if(spin.getSelectedItem().equals(0))
                    {
                        Double addprice = 15.0;
                        Totalprice.setText(Double.toString(currentprice)+ 
                        Double.toString(addprice));
                    }
                }

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

What I trying to do is when user select Item A from spinner, the Totalprice edittext box will add the price on it . For example , the current price is 0.0 and user select Item A ,it will become displaying 10.0. This apply to other Item as well, which meaning whenever user select any item, the price will auto add to Totalprice.

I successfully add the spinner but when I select any item on spinner and it just won't work, anyone can provide solution or other better way to do this?

Aucun commentaire:

Enregistrer un commentaire