-1

我的萨尔曼·阿姆贾德我在我的应用程序中使用了两个微调器,并且在每个选择上都使用了 if else 结构......但是当应用程序加载时,默认选择条件执行......我想删除默认选择可以任何一个你请帮我做这件事????第一个条件也不会在第一个执行....

 convertto.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View v1,
                int index1, long arg3) {
            //from meters
            var1=Double.parseDouble(lvalue.getText().toString());
            var2=var1;
            if ((quantity[index1]=="Meter") && (s1.equalsIgnoreCase("Meter") ))
            {
            Toast.makeText(v1.getContext(), " to meter",Toast.LENGTH_SHORT).show();
            var2=var1;
            lresult.setText("" + var2);

        }
            else{
                if ((quantity[index1]=="Inches") && (s1.equalsIgnoreCase("Meter")))
                {
                Toast.makeText(v1.getContext(), " to inches",Toast.LENGTH_SHORT).show();

                var2=var1 * 39.3700787;
                lresult.setText("" + var2);

            }
                else{
                    if ((quantity[index1]=="Feet") && (s1.equalsIgnoreCase("Meter")))
                    {
                    Toast.makeText(v1.getContext(), " to Feet",Toast.LENGTH_SHORT).show();

                    var2=var1 * 3.281;
                    lresult.setText("" + var2);

                }
                    else{
                        if ((quantity[index1]=="Yards") && (s1.equalsIgnoreCase("Meter")))
                        {

                        Toast.makeText(v1.getContext(), " to Yards",Toast.LENGTH_SHORT).show();

                        var2=var1 * 1.0936133 ;
                        lresult.setText("" + var2);
                        }
                    }
                }
                //from inches
                if ((quantity[index1]=="Meter") && (s1.equalsIgnoreCase("Inches")))
                {
                Toast.makeText(v1.getContext(), " to meter",Toast.LENGTH_SHORT).show();

                var2=var1/39.3700787;
                lresult.setText("" + var2);

            }
                else{
                    if ((quantity[index1]=="Inches") && (s1.equalsIgnoreCase("Inches")))
                    {
                    Toast.makeText(v1.getContext(), " to inches",Toast.LENGTH_SHORT).show();

                    var2=var1;
                    lresult.setText("" + var2);

                }
                    else{
                        if ((quantity[index1]=="Feet") && (s1.equalsIgnoreCase("Inches")))
                        {
                        Toast.makeText(v1.getContext(), " to Feet",Toast.LENGTH_SHORT).show();

                        var2=var1/12;
                        lresult.setText("" + var2);

                    }
                        else{
                            if ((quantity[index1]=="Yards") && (s1.equalsIgnoreCase("Inches")))
                            {

                            Toast.makeText(v1.getContext(), " to Yards",Toast.LENGTH_SHORT).show();

                            var2 = var1/36;
                            lresult.setText("" + var2);
                            }
                        }
                    }
                    //from feet
                    if ((quantity[index1]=="Meter") && (s1.equalsIgnoreCase("Feet")))
                    {
                    Toast.makeText(v1.getContext(), " to meter",Toast.LENGTH_SHORT).show();

                    var2=var1/3.281;
                    lresult.setText("" + var2);

                }
                    else{
                        if ((quantity[index1]=="Inches") && (s1.equalsIgnoreCase("Feet")))
                        {
                        Toast.makeText(v1.getContext(), " to inches",Toast.LENGTH_SHORT).show();

                        var2=var1*12;
                        lresult.setText("" + var2);

                    }
                        else{
                            if ((quantity[index1]=="Feet") && (s1.equalsIgnoreCase("Feet")))
                            {
                            Toast.makeText(v1.getContext(), " to Feet",Toast.LENGTH_SHORT).show();

                            var2=var1;
                            lresult.setText("" + var2);

                        }
                            else{
                                if ((quantity[index1]=="Yards") && (s1.equalsIgnoreCase("Feet")))
                                {

                                Toast.makeText(v1.getContext(), " to Yards",Toast.LENGTH_SHORT).show();

                                var2 = var1/3;
                                lresult.setText("" + var2);
                                }
                            }
                        }
                        //from yards
                        if ((quantity[index1]=="Meter") && (s1.equalsIgnoreCase("Yards")))
                        {
                        Toast.makeText(v1.getContext(), " to meter",Toast.LENGTH_SHORT).show();

                        var2=var1/1.0936133;
                        lresult.setText("" + var2);

                    }
                        else{
                            if ((quantity[index1]=="Inches") && (s1.equalsIgnoreCase("Yards")))
                            {
                            Toast.makeText(v1.getContext(), " to inches",Toast.LENGTH_SHORT).show();

                            var2=var1*36;
                            lresult.setText("" + var2);

                        }
                            else{
                                if ((quantity[index1]=="Feet") && (s1.equalsIgnoreCase("Yards")))
                                {
                                Toast.makeText(v1.getContext(), " to Feet",Toast.LENGTH_SHORT).show();

                                var2=var1*3;
                                lresult.setText("" + var2);

                            }
                                else{
                                    if ((quantity[index1]=="Yards") && (s1.equalsIgnoreCase("Yards")))
                                    {

                                    Toast.makeText(v1.getContext(), " to Yards",Toast.LENGTH_SHORT).show();

                                    var2 = var1;
                                    lresult.setText("" + var2);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
4

1 回答 1

1

这是因为当您第一次执行您的应用程序onItemSelected并被解雇时。为了摆脱这种情况,您需要自己应用一些逻辑东西,以便当 Activity 第一次启动时它不会触发您的代码,onItemSelected并且在用户进行选择时工作。

你可以做类似的事情,

int spinner_counter = 1, spinnner_selected = 0; // declare globally

        @Override
        public void onItemSelected(AdapterView<?> arg0, View v1,
                int index1, long arg3) {

           if(spinnner_selected < spinner_counter){
                  spinnner_selected++;
           }
           else{
                 //execute your code here
           }
        }
于 2012-07-05T04:56:45.620 回答