【问题标题】:Multiplying spinner value with array value将微调器值与数组值相乘
【发布时间】:2016-06-13 18:17:04
【问题描述】:

这是我的代码,我想将微调器的值与数组值相乘。

该数组从我的另一个名为 Constant 的 java 类中获取其值,并且该类有一个数组 food calories

protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.items_details);
        Spinner mspin=(Spinner) findViewById(R.id.spinner1);

        Integer[] items = new Integer[]{1,2,3,4};
        ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_item, items);
        mspin.setAdapter(adapter);

        TextView name_select=(TextView)findViewById(R.id.SelectedName);
                name_select.setText(constant.food_items[constant.SelectedIndex]);
        imageView =  (ImageView) findViewById(R.id.imagedetail);
        UpdateImage(constant.food_items[constant.SelectedIndex]);

        TextView calories=(TextView)findViewById(R.id.calories111);
                calories.setText(constant.food_calories[constant.index]+"");

        int multiple=0;

        {

             try {multiple= mspin.getSelectedItemPosition()*       constant.food_calories[constant.index];

          TextView tot_calories=(TextView)findViewById(R.id.caloriestotal);
          tot_calories.setText(multiple+"");}

          catch(NumberFormatException nfe) {
              Log.e("Error", "Failed to multiply invalid non-numbers");
                   }

常量类代码

public static final Integer[] food_calories=new Integer[]{74
};
    public static int index = 0;

}

【问题讨论】:

  • 你的常量类是什么?添加此类代码。
  • @HaniyehKhaksar 看到这个常量类代码
  • 您的数组中没有任何数据!
  • 是的 public static final Integer[] food_calories=new Integer[]{74 }

标签: java android arrays spinner


【解决方案1】:

您可以使用以下代码解决此问题:

//Make these variables global so that it can be accessed in onClickListner.
Integer[] items = new Integer[]{1,2,3,4};
int multiple=0;
ImageView imageView;
TextView tot_calories;
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.items_details);
      Spinner mspin=(Spinner) findViewById(R.id.spinner1);
      ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_item, items);
      mspin.setAdapter(adapter);

      TextView name_select=(TextView)findViewById(R.id.SelectedName);
      name_select.setText(constant.food_items[constant.SelectedIndex]);
      imageView =(ImageView) findViewById(R.id.imagedetail);
      UpdateImage(constant.food_items[constant.SelectedIndex]);
      TextView calories=(TextView)findViewById(R.id.calories111);
      calories.setText(constant.food_calories[constant.index]+"");

      tot_calories=(TextView)findViewById(R.id.caloriestotal);
      mspin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()     {
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
      // Your code here
            multiple=items[i]*constant.food_calories[constant.index];
            tot_calories.setText("update Value is: "+multiple);
      } 

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

  });
  }

更新了我的答案!因为我已经测试了自己,所以它是完整的解决方案。

【讨论】:

  • 什么都没发生?
  • 是否在更新 tot_calories textview 中的文本?
【解决方案2】:

您需要将设置multiple 变量值的代码放在微调器mspin 的侦听器中,以便每次更改微调器时都会调用它。在onCreate方法中添加如下代码

        mspin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            //Your code to set multiple's value and calculate total calories here...
            try {
                multiple = (int)parent.getItemAtPosition(position) * constant.food_calories[constant.index];
                TextView tot_calories = (TextView) findViewById(R.id.caloriestotal);
                tot_calories.setText(multiple + "");
            } catch (NumberFormatException nfe) {
                Log.e("Error", "Failed to multiply invalid non-numbers");
            }
        }

        /**
         * Callback method to be invoked when the selection disappears from this view. 
         * The selection can disappear for instance when touch is activated or when the adapter becomes empty.
         * @param arg0
         */
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // You can also do something here, but not necessary
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多