将微调器值与数组值相乘
Multiplying spinner value with array value
这是我的代码,我想将微调器的值与数组值相乘。
该数组从我的另一个名为 Constant
的 java class 获取其值,并且 class 有一个数组 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");
}
常数class代码
public static final Integer[] food_calories=new Integer[]{74
};
public static int index = 0;
}
您可以使用以下代码解决此问题:
//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;
}
});
}
更新了我的答案!因为这是我自己测试过的完整解决方案。
您需要将设置 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
}
});
这是我的代码,我想将微调器的值与数组值相乘。
该数组从我的另一个名为 Constant
的 java class 获取其值,并且 class 有一个数组 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");
}
常数class代码
public static final Integer[] food_calories=new Integer[]{74
};
public static int index = 0;
}
您可以使用以下代码解决此问题:
//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;
}
});
}
更新了我的答案!因为这是我自己测试过的完整解决方案。
您需要将设置 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
}
});