-1

I'm asking for help to analyze my algorithm and try to fix what am I missing or doing something wrong. At first when I just add items on the "Cart", its price adds up to the total amount. When I delete some of the items, it does do its subtraction without any problem. But when there is one item left in the "Cart", for example, the price costs $20, so the total amount is $20. When I delete the remaining item, my "Cart" is empty but the total amount is still $20.

Here is my Cart.class where I set the text in my total amount

package com.example.claude.afinal;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;

public class Cart extends MainActivity {

TextView amount_total;
ListView cartList;
CartCustomAdapter cartCustomAdapter;
String name, price;
static ArrayList<Order> cartArray = new ArrayList<Order>();
static Double total_amount = 0.00d;
static Double temp = 0.00d;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cart);
    amount_total = (TextView) findViewById(R.id.total_tv);
    Bundle bundle = getIntent().getExtras();
    Button checkout =  (Button) findViewById(R.id.check_out);
    Button add_item = (Button) findViewById(R.id.add_item);
    name = bundle.getString("i_name");
    price = bundle.getString("i_price");
    temp = Double.parseDouble(price);
    total_amount = (total_amount + temp);

    add_item.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Cart.this,MainActivity.class);
            startActivity(intent);
        }
    });

    cartList = (ListView) findViewById(R.id.cart_list);
    cartCustomAdapter = new CartCustomAdapter(Cart.this,R.layout.list_cart,cartArray);
    cartList.setItemsCanFocus(false);
    cartList.setAdapter(cartCustomAdapter);
    cartArray.add(new Order(name,price,"1"));
    cartCustomAdapter.notifyDataSetChanged();
}

public void change_total(int y, Double result) {
    if (y == 0) {
        amount_total.setText(total_amount.toString());
    } else {
        total_amount = total_amount - result;
        amount_total.setText(total_amount.toString());
    }         
}

And here is my CarCustomAdapter with listview for the items and with delete button

package com.example.claude.afinal;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;

public class CartCustomAdapter extends ArrayAdapter<Order> {
    Context context;
    int layoutResourceId;
    ArrayList<Order> items = new ArrayList<Order>();
    Integer counter = 0;
    Double x  = 0.00d;
    Boolean clicked = false;
    Double y = 0.00d;

    public CartCustomAdapter(Context context, int layoutResourceId,
    ArrayList<Order> items) {
        super(context, layoutResourceId, items);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.items = items;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)    {
        View row = convertView;
        UserHolder holder = null;

        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new UserHolder();
            holder.cart_name = (TextView) row.findViewById(R.id.tv_cart_name);
            holder.cart_qty = (TextView) row.findViewById(R.id.tv_cart_qty);
            holder.cart_price = (TextView) row.findViewById(R.id.tv_cart_price);
            holder.del = (Button) row.findViewById(R.id.del_item_button);
            row.setTag(holder);
        } else {
            holder = (UserHolder) row.getTag();
        }
        final Order data = items.get(position);
        holder.cart_name.setText(data.getName());
        holder.cart_price.setText(data.getPrice());
        holder.cart_qty.setText(data.getQty());
        x = x + Double.parseDouble(data.getPrice());
        holder.del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                remove(getItem(position));
                y = Double.parseDouble(data.getPrice());
                clicked = true;
            }
        });
        if(clicked){
            ((Cart)getContext()).change_total(1,y);
        }
        else
        {
            ((Cart)getContext()).change_total(0,x);
        }

        return row;
     }

     static class UserHolder {
         TextView cart_name;
         TextView cart_qty;
         TextView cart_price;
         Button del;
     }  
}
Vadim Kotov
  • 7,103
  • 8
  • 44
  • 57
nerdy kid
  • 153
  • 2
  • 9
  • Obviously changing x inside getView doesn't make sense it may be called multiple times for the same item. – Selvin Jan 19 '17 at 18:07
  • please read. it does subtract. the only problem is when it comes to deleting the last item. – nerdy kid Jan 20 '17 at 05:00
  • Ok you are the master of android ... return when you get that it will not work. fx after adding so many items that you had too scroll the ListView ... also you obviously don't know java basics like code flow (you think that `if(clicked){` code is called magically after onClick :) – Selvin Jan 20 '17 at 05:01
  • hahaha. I guess you have better future on 9gag. Unbelievable. – nerdy kid Jan 20 '17 at 05:14
  • *I guess you have better future on 9gag* What for ... I had a lots of fun reading your code – Selvin Jan 20 '17 at 05:15

2 Answers2

0

I can see this code is executed at the very end of the getView method

if(clicked){
    ((Cart)getContext()).change_total(1,y);

}
else
{
    ((Cart)getContext()).change_total(0,x);
}

When the listview has no items, the getView method may not be getting called, so you should try simply moving the ((Cart)getContext()).change_total(1,y); line to your delete button click listener, like this:

remove(getItem(position));
y = Double.parseDouble(data.getPrice());
clicked = true;
((Cart)getContext()).change_total(1,y);

That's my guess.

Juan Martinez
  • 705
  • 5
  • 14
  • Yeah it will help... Temporary... if there would be more items than may be visible then of course after scrolling the total would be f.. up even worst – Selvin Jan 19 '17 at 18:17
  • yes. I guess you're right Juan. because it does do check the row if null. is there a way to get rid of the condition to check the row? I guess that's the problem to my algorithm. – nerdy kid Jan 20 '17 at 05:05
  • @Juan thanks for giving an idea. I changed some codes and it finally worked. – nerdy kid Jan 20 '17 at 05:32
  • @nerdykid, the null check on the getView convertView is part of the ViewHolder pattern. You can read a little more about it on [this question](http://stackoverflow.com/questions/19289812/findviewbyid-vs-view-holder-pattern-in-listview-adapter) – Juan Martinez Jan 20 '17 at 12:43
-1

In your function change_total, why don't you simply do this:

    if (y == 0) {
        total_amount  = 0.0;
        amount_total.setText("0");
    }

So, if there are no items, simply set the cart total to 0

Ankur Aggarwal
  • 2,146
  • 2
  • 31
  • 35
  • Obviously because 0 means only that item was deleted... Not always it would be the last one – Selvin Jan 19 '17 at 18:14