I am trying to make an e-commerce app. When I click on the ADD "+" sign, Quantity and sub-total is calculated on the basis of price.Please assist me in re-initializing the variable for each item of ListView. Its working fine for first Item.


I am trying to make an e-commerce app. When I click on the ADD "+" sign, Quantity and sub-total is calculated on the basis of price.Please assist me in re-initializing the variable for each item of ListView. Its working fine for first Item.


create a class and declare its members accordingly. now whenever you need to add a new row create the object of that class and assign values to them.
example:
create a class named MyEntity
public class MyEntity {
String entity = "";
double price = 0.0;
int quantity = 0;
double subTotal = 0.0;
public MyEntity(String entity, double price, int quantity, double subTotal) {
this.entity = entity;
this.price = price;
this.quantity = quantity;
this.subTotal = subTotal;
}
}
usage:
Create a List in your activity or wherever you are creating the list as
List<MyEntity> myEntityList = new ArrayList<>();
on click of add button create the object of MyEntity class like
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String q = (String) quantity.getText();
Double d = Double.parseDouble(q);
qty=d;
qty++;
Double sub_total =currentItem.getPrice();
item_sub_total = qty*sub_total;
MyEntity myEntity = new MyEntity("Onion", sub_total, qty, item_sub_total);
myEntityList.add(myEntity);
}
});
pass this list to your list view adapter and use accordingly
you can access the fields like myEntity.price or myEntity.subTotal etc.
I resolved this. But I am not satisfied. Can anyone Improve it by using List or Array???
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String q= (String) quantity.getText();
Double d = Double.parseDouble(q);
qty=d;
qty++;
Double sub_total =currentItem.getPrice();
item_sub_total = qty*sub_total;
quantity.setText(String.valueOf(qty));
subtotal.setText(String.valueOf(item_sub_total));
}
});