0

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.

enter image description here

enter image description here

2
  • you can just make the changes in your data and then can refresh your list view. Commented Aug 18, 2017 at 12:56
  • add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Long sub_total =currentItem.getPrice(); item_sub_total = qty*sub_total; quantity.setText(String.valueOf(qty)); subtotal.setText(String.valueOf(item_sub_total)); } }); Commented Aug 18, 2017 at 13:47

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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));
                }
            });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.