从微调器获取值并将其放入Android中的数组中

时间:2017-04-30 02:05:52

标签: java android arrays spinner

我正在尝试使用Spinner创建一个下拉菜单,用户可以在其中选择一个数字。然后,用户单击的数字将被放入一个数组中,程序将计算数组内的总数。但它无法将所选项添加到数组中。我一直在浏览很多文章;但是,我找不到解决方案。

这是我的代码:

更新:我能够找到将所选项目放入数组列表的解决方案,但我无法获得数组中总项目的结果。

    public class MainActivity extends AppCompatActivity {
        //Declare an array to store units
        public static ArrayList<Double> unitsAdd = new ArrayList<Double>();
        //Declare spinner
        Spinner spinner;
        //Declare adapter to connect array in string value
        ArrayAdapter<CharSequence> adapter;
        //
        Double num1, totalUnits;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            spinner = (Spinner) findViewById(R.id.spinner);

            adapter = ArrayAdapter.createFromResource(MainActivity.this, R.array.units,
                    android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            //display the adapter contain the list
            spinner.setAdapter(adapter);

            //delcare buttons
            Button bntInsert = (Button) findViewById(R.id.bntInsert);
            Button bntCount = (Button) findViewById(R.id.bntCount);
            //Declare textView
            final TextView resultTotalUnits = (TextView) findViewById(R.id.resultTotalUnits);
            //Insert function
            bntInsert.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    /*Removed This line of codes
               spinner.setOnItemSelectedListener(new 
               AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> list, View view, int position, long id) {
                            //define selected unit. I am missing something right here
                            String unitSelected = spinner.getSelectedItem().toString();
                            //convert string to double
                            num1 = Double.parseDouble(unitSelected);
                            //add to array but it does not work
                            unitsAdd.add(num1);
                            Toast.makeText(getApplicationContext(),
                                    "You have inserted: " + num1 + " units",
                                    Toast.LENGTH_SHORT).show();

                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {

                        }
                    });*/
//Added these
String unitSelected = spinner.getSelectedItem().toString();
                num1 = Double.parseDouble(unitSelected);
                unitsAdd.add(num1);
Toast.makeText(getApplicationContext(),
                       "You have inserted: " + unitsAdd + " units",
                       Toast.LENGTH_SHORT).show();
                }
            });
    //When clicking the button, the app stopped working
           bntCount.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   for(int i=0;i<unitsAdd.size();i++){
                       //sum of the array called units
                       totalUnits =  (unitsAdd.get(i)) + totalUnits;
                   }
                   //Display the results in the TextView sections
                   resultTotalUnits.setText("Total units :"+Double.toString(totalUnits));
               }
           });

        }
    }

2 个答案:

答案 0 :(得分:0)

错误在此行

String unitSelected = spinner.getSelectedItem().toString();

微调器不保持所选值,AdapterView函数之一的onSelected()保持所选值。

将该陈述改为

String unitSelected = list.getSelectedItem().toString();

编辑:

我认为实施有些不对劲。试试这个:

  1. 声明一个静态双变量,它将保存微调器选择的值。

  2. 然后为Spinner实现一个普通的Adapter。每次选择微调器时,在适配器中获取该值并将其存储在静态变量中。

  3. 单击按钮时,从静态变量中获取值并将其添加到ArrayList

  4. 示例代码:

    Static Double selectedDouble;
    
    spinner.setOnItemSelectedListener(myListener);
    AdapterView.OnItemSelectedListener myListener = new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    String item = adapterView.getItemAtPosition(i).toString();
                    selectedDouble = Double.parseDouble(item);
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                adapterView.setSelection(0);
                selectedDouble = Double.parseDouble("Some Default Value");
    
            }
        };
    
    btn.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                 DoubleArray.add(selectedDouble);
            }
    
            });
    

    在对其执行任何操作之前,将可变总计单元初始化为0。最好是onCreate()

    totalUnits = 0.00;

答案 1 :(得分:0)

删除不需要的此电话:

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

相反,请将android.R.layout.simple_spinner_dropdown_item传递给

adapter = ArrayAdapter.createFromResource(MainActivity.this, R.array.units,
            android.R.layout.simple_spinner_dropdown_item);