我的xml中有3个微调器..
1. one is Product Name
2. one is Product Quantity.
3. and 3rd one is Discount
现在,我为每一个调用单独的Listener ..问题是我想使用项目数量来计算最终价格,我在所有的同一序列中调用了微调器,这是我写的名称,数量和折扣..
数量的听众最后被调用,即使我在上面写了折扣Spinner ..我不知道发生了什么..可能有人遇到过这个问题......请帮助我..
这是我的Spinner听众: -
spnSelectItem.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
strItem=spnSelectItem.getItemAtPosition(arg2).toString(); }
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
///This method called last..
spnQty.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
strQty=spnQty.getItemAtPosition(arg2).toString();
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
///This method called before spnQty ..
spnItemDiscount.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
disc=round(Double.parseDouble(spnItemDiscount.getItemAtPosition(arg2).toString().replace("%", "")));
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
答案 0 :(得分:4)
为什么不创建一个单独的方法来执行所有这样的计算:
private void calculatePrice() {
String strItem = spnSelectItem.getSelectedItem().toString();
String qty = spnQty.getSelectedItem().toString();
String discount = spnItemDiscount.getSelectedItem().toString().replace("%", "");
//perform calculate and update UI as you like
}
然后为所有三个微调器创建一个侦听器,或者在所有三个微调器中调用它们,例如:
spnSelectItem.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//do some work, update UI or reset qty and discount spinner or whatever
strItem = spnSelectItem.getItemAtPosition(arg2).toString();
//call calulcate method
calculatePrice();
}
});