如何解决递减运算符在android studio中不起作用
我想在按下 + 和 - 按钮时提高和降低价格。在这里,我的价格在上涨,+ 按钮工作正常。但是,当我按下 - 按钮时,只有数量在减少,而价格在上涨。我已从 sqlite 数据库中检索到产品价格。
public class Sofa1 extends AppCompatActivity {
ImageSlider sofaslider1;
ImageButton plusquantity,minusquantity;
TextView quantitynumber,sofaname,sofaprice,sofadesc;
Button addtocart;
int quantity;
int totalprice=0;
public Uri mcurrentcarturi;
boolean hasallrequiredvalues = false;
DBHelper DB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sofa1);
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
sofaname=findViewById(R.id.sofaname);
sofaprice=findViewById(R.id.sofaprice);
sofadesc=findViewById(R.id.sofadesc);
plusquantity = findViewById(R.id.addquantity);
minusquantity = findViewById(R.id.subquantity);
quantitynumber = findViewById(R.id.quantity);
DB = new DBHelper(Sofa1.this);
String Name = DB.getProductNamePrice("SELECT F_Name FROM Furniture WHERE F_Type = 'Sofa';");
String Price = DB.getProductNamePrice("SELECT F_Price FROM Furniture WHERE F_Type = 'Sofa';");
String Desc = DB.getProductNamePrice("SELECT F_Description FROM Furniture WHERE F_Type = 'Sofa';");
sofaname.setText(Name);
sofaprice.setText(Price);
sofadesc.setText(Desc);
plusquantity.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(quantity<5){
//sofaprice
int baseprice= Integer.parseInt(sofaprice.getText().toString());
quantity++;
displayquantity();
totalprice = baseprice * quantity;
String setnewprice = (String.valueOf(totalprice));
sofaprice.setText(setnewprice);
}
}
});
minusquantity.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int baseprice= Integer.parseInt(sofaprice.getText().toString());
if(quantity>1) {
quantity--;
displayquantity();
totalprice = baseprice * quantity;
String setnewprice = (String.valueOf(totalprice));
sofaprice.setText(setnewprice);
}
}
});
}
private void displayquantity() {
quantitynumber.setText(String.valueOf(quantity));
}
}
解决方法
您正在使用这行代码读取基本价格:
int baseprice= Integer.parseInt(sofaprice.getText().toString());
但这不对。沙发价格包含计算价格,而不是基本价格。
我想要的是:
-
创建一个变量,int baseprice,初始化为零;
-
当您从数据库中读取基价时,将基价设置为该值:
String Price = DB.getProductNamePrice("SELECT F_Price FROM Furniture WHERE F_Type = 'Sofa';"); baseprice = Integer.parseInt(Price);
-
当您增加或减少数量时,将使用活动级别的基价计算总价。您不需要从 textview 中再次阅读它(这样做是错误的)。
根据你的代码,每次点击按钮,价格会取当前总价,而不是单价。并且每个价格是 totalPice * quantity
,实际总价格应该是 singlePice * quantity
因此您应该将单价设置为全局变量,例如
private int unitPrice;
...
String Price = DB.getProductNamePrice("SELECT F_Price FROM Furniture WHERE F_Type = 'Sofa';");
unitPrice = Integer.parseInt(Price)
...
plusquantity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(quantity<5){
//sofaprice
quantity++;
displayquantity();
totalprice = unitPrice * quantity;
String setnewprice = (String.valueOf(totalprice));
sofaprice.setText(setnewprice);
}
}
});
minusquantity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(quantity>1) {
quantity--;
displayquantity();
totalprice = unitPrice * quantity;
String setnewprice = (String.valueOf(totalprice));
sofaprice.setText(setnewprice);
}
}
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。