2016-03-01 98 views
0

這裏是我的代碼,我想將微調器的值與數組值相乘。將微調值與數組值相乘

該陣列由命名爲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; 

}

+0

什麼是您的常數班?添加此類代碼。 –

+0

@HaniyehKhaksar看到這個不變的類代碼 – user3343999

+0

你的數組中沒有任何數據! –

回答

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; 
     } 

    }); 
    } 

更新了我的答案!因爲現在我已經完成了自己的測試。

+0

:(happind – user3343999

+0

你是什麼意思都沒有發生? – Hamad

+0

是它tot_calories的TextView更新的文字? – Hamad

0

您需要將代碼設置爲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 
     } 
    });