[英]How to set the text of TextView based on Spinner?
My application has TextView and Spinner. 我的应用程序具有TextView和Spinner。 Spinner contains 2 items (A and B).
微调框包含2个项目(A和B)。 How do I set the text of TextView based on the selection of Spinner ?
如何根据Spinner的选择设置TextView的文本? All these have to happen in one activity.
所有这些都必须在一项活动中发生。
if(categoryType.getSelectedItem() == A){
taxFee.setText("Total tax fee:RM" + foodFee);
}else{
taxFee.setText("Total tax fee:RM" + groceryFee);
}
I have tried the code above, it doesn't show the result I want. 我已经尝试了上面的代码,但没有显示我想要的结果。 It always shows this statement
taxFee.setText("Total tax fee:RM" + foodFee)
even if I select other category. 即使我选择其他类别,它始终显示此语句
taxFee.setText("Total tax fee:RM" + foodFee)
。
It depends on your requirements, you can try this approach using the position: 这取决于您的要求,您可以在以下位置尝试这种方法:
categoryType.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
if(position == 0)
{
taxFee.setText("Total tax fee:RM" + foodFee);
}else{
taxFee.setText("Total tax fee:RM" + groceryFee);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
//something
}
});
使其成为if(categoryType.getSelectedItem()。toString()。equals(A))
Why don't you put A in double quotes? 为什么不将A放在双引号中?
if(categoryType.getSelectedItem() == "A"){
taxFee.setText("Total tax fee:RM" + foodFee);
}else{
taxFee.setText("Total tax fee:RM" + groceryFee);
}
or this 或这个
if(categoryType.getSelectedItem().toString().equals("A"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.