0
   package com.hotel;

   import java.util.ArrayList;
   import android.app.Activity;
   import android.content.res.Resources;
   import android.view.LayoutInflater;
   import android.view.View;
   import android.view.View.OnClickListener;
   import android.view.ViewGroup;
   import android.widget.ArrayAdapter;
   import android.widget.Button;
   import android.widget.ImageView;
   import android.widget.TextView;
   import android.widget.Toast;

   public class CustomOrderedItemAdapter extends ArrayAdapter {
   Activity activity;
   ArrayList orderedList;
   Resources res;

   final int price = 50;
   final int qty = 1;
   static int total;

   public CustomOrderedItemAdapter(Activity activity, ArrayList orderedList,
        Resources res) {
    super(activity, R.layout.ordered_item_cell, orderedList);
    this.activity = activity;
    this.orderedList = orderedList;
    this.res = res;
  }
  public static class ViewHolder {
    TextView textOrderedItem, textItemQty, textOrderedItemPrice;
    ImageView orderedItemImg;
    Button btnUp, btnDown, btnDeleteOrderedItem;
    //int total;
  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    final int deletePosition = position;
    final ViewHolder holder;
    LayoutInflater inflater = (LayoutInflater) activity
            .getSystemService(activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.ordered_item_cell, null);
        holder = new ViewHolder();

        holder.textOrderedItem = (TextView) convertView
                .findViewById(R.id.textOrderedItem);
        holder.orderedItemImg = (ImageView) convertView
                .findViewById(R.id.orderedItemImg);

        holder.textItemQty = (TextView) convertView
                .findViewById(R.id.textItemQty);
        holder.textItemQty.setText("1");

        holder.btnUp = (Button) convertView.findViewById(R.id.btnUp);
        holder.btnDown = (Button) convertView.findViewById(R.id.btnDown);

        holder.textOrderedItemPrice = (TextView) convertView
                .findViewById(R.id.textOrderedItemPrice);
        holder.textOrderedItemPrice.setText("50");

        holder.btnDeleteOrderedItem = (Button) convertView
                .findViewById(R.id.btnDeleteOrderedItem);


        total=total+(price*qty);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.btnUp.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            int qty = Integer.parseInt(holder.textItemQty.getText()
                    .toString()) + 1;
            holder.textOrderedItemPrice.setText("" + (price * qty));
            holder.textItemQty.setText(String.valueOf(qty));

            total=total+price;
            Toast.makeText(activity,""+total, Toast.LENGTH_LONG).show();

        }
    });

    holder.btnDown.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            int qty = Integer.parseInt(holder.textItemQty.getText()
                    .toString());
            if (qty > 1) {
                qty -= 1;
                holder.textOrderedItemPrice.setText("" + price * qty);
                holder.textItemQty.setText("" + qty);
                total=total-price;
                Toast.makeText(activity,""+total, Toast.LENGTH_LONG).show();
            }
        }
    });
    holder.btnDeleteOrderedItem.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            orderedList.remove(deletePosition);
            notifyDataSetChanged();
        }
    });

    holder.textOrderedItem.setText(orderedList.get(position).toString());
    return convertView;
   }

   }

I want to do total of all item price , but i got my total greater than my actual
ex: if i have 4 item , and it's price 50 , i got 250 total instead of 200, and explain me how to call automatically getView() method , from where it is calling , Thanks

  • r u trying to display the total as part of the list. getView gets called for as many times as the visible rows of the list for the first time and then the views are recycled – chaitanya Jul 08 '14 at 06:16
  • try commneting total=total+price; statement – user3717646 Jul 08 '14 at 06:18
  • Actually, ListView renders it's view to only the visible screen area, after it, getView will be calls to create/get the new views each time you scroll the list. – jitain sharma Jul 08 '14 at 06:59
  • [getView() will be called multiple times as you note. it shouldn't matter, because your array adapter is based on the state of it's internal data model (array, list of objects, whatever). getView() should be idempotent in that calling it multiple times shouldn't change the result](http://stackoverflow.com/questions/9853397/android-custom-arrayadapter-getview-method-called-multiple-times-resetting-dyn) – Dinesh Krishnan Jul 12 '16 at 06:43

4 Answers4

1

Do not couple business logic with presentation.

getView gets called every time the view is drawn. You don't have control over it's invocation and should not be making changes in the model or perform calculations in it.

You can just make a separate function that loops on the items and calculates total.

vipul mittal
  • 17,343
  • 3
  • 41
  • 44
1

That's not how getView should be used. getView is called by the ListView whenever it needs to assign a position to a view. It does not call each position once. It does not promise to call anything in order. You should not be doing any calculations in getView that depend on any other position- the only thing you should be doing is setting the UI for the row.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
0

once you call the CustomOrderedItemAdapter class the getview() method will invoke automatically by the class and whenever getView is called by the ListView it needs to assign a position to a view. so you no need to call the method name getview()

0

You are initializing total as 50 just above convertView. As qty is initialized as 1 and price is 50. So total is 50.

Now when you are adding qty using btnUp. As total is 50. it gets added. Initialize total as 0 and it should work fine.

    total=total+(price*qty); //50

    convertView.setTag(holder);
Yatish
  • 1
  • 1