1

I have a TextView outside ListView and i need to add prices when the plus button (ie,quantity is incremented )in ListView is clicked.In my program i am not able to add prices when new position ListView button is clicked.I need to find the total price to be payed by the customer when plus button is clicked in ListView

    public class ListAdapter1 extends BaseAdapter {
public int qty=1;

    public ArrayList<Integer> quantity = new ArrayList<Integer>();
    private TextView total;
    private String[] listViewItems,prices,weight;
    TypedArray images;
    public static int pValue;
    private Context context;
    public static boolean t=false;
    CustomButtonListener customButtonListener;

    public void setTextView(TextView total)
    {
        this.total = total;
    }



    public ListAdapter1(Context context, String[] listViewItems, TypedArray images, String[] weight, String[] prices) {
        this.context = context;
        this.listViewItems = listViewItems;
        this.images = images;
        this.prices=prices;
        this.weight=weight;
    }

    public void setCustomButtonListener(CustomButtonListener customButtonListner)
    {
        this.customButtonListener = customButtonListner;
    }

    @Override
    public int getCount() {
        return 5;
    }

    @Override
    public String getItem(int position) {
        return listViewItems[position];
    }


    @Override
    public long getItemId(int position) {
        return 0;
    }

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


        final ListViewHolder listViewHolder;
        if(convertView == null)
        {
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = layoutInflater.inflate(R.layout.product,parent,false);
            listViewHolder = new ListViewHolder();
            listViewHolder.tvProductName = (TextView) row.findViewById(R.id.tvProductName)
            listViewHolder.tvPrices = (TextView) row.findViewById(R.id.tvProductPrice);
            listViewHolder.btnPlus = (ImageButton) row.findViewById(R.id.ib_addnew);
            listViewHolder.edTextQuantity = (EditText) row.findViewById(R.id.editTextQuantity);
            listViewHolder.btnMinus = (ImageButton) row.findViewById(R.id.ib_remove);
            row.setTag(listViewHolder);
        }
        else
        {
            row=convertView;
            listViewHolder= (ListViewHolder) row.getTag();
        }

        try{

            listViewHolder.edTextQuantity.setText(quantity.get(position) );

        }catch(Exception e){
            e.printStackTrace();
        }


        listViewHolder.btnMinus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Toast.makeText(context, " " + position, Toast.LENGTH_SHORT).show();

                int mValue = Integer.parseInt(listViewHolder.edTextQuantity.getText().toString());
                if (mValue <=0) {
                    System.out.println("not valid");
                    mValue=0;
                    listViewHolder.edTextQuantity.setText("" +mValue);
                }
                else{
                    pValue=pValue/mValue;
                    mValue--;
                    pValue=pValue*mValue;
                    total.setText(String.valueOf(pValue));
                    System.out.println("mvalue after reducing-----------"+mValue);
                    System.out.println("pvalue-----------"+pValue);
                    listViewHolder.edTextQuantity.setText( "" +mValue );

                }
            }
        });

        listViewHolder.btnPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Toast.makeText(context, " " + position, Toast.LENGTH_SHORT).show();

                int mValue = Integer.parseInt(listViewHolder.edTextQuantity.getText().toString());
                pValue=Integer.parseInt(listViewHolder.tvPrices.getText().toString());
                mValue++;
                listViewHolder.edTextQuantity.setText("" + mValue);
                System.out.println("mValue after increment---" + mValue);
                pValue=pValue*mValue;
                System.out.println("pvalue-----------"+pValue);
                total.setText(String.valueOf(pValue));
            }
        });
        return row;
    }

I need to get total price when any of the ListView button is clicked.

Jas
  • 3,207
  • 2
  • 15
  • 45
Lena
  • 21
  • 6

2 Answers2

1

First you need to store value in HashMap<> when user click the plus and minus button.

Then sum the all values in HashMap.

For Example

   try{
   int sum = 0;
   for(HashMap<String, String> map : arrayList) {
      sum += Integer.parseInt(map.get("mark"));
   }
} catch (Exception e) {
   //Manage your exception
}
// sum has the value for the marks total.
System.out.println("Total Marks: "+sum);

Refere my previous answer Here

Community
  • 1
  • 1
Arpit Patel
  • 7,212
  • 5
  • 56
  • 67
0

For that you need to create interface which notify in activity where you want that count.

put snippet in adapter to initialize interface and setter.

    public interface IEvent {
                void onItemChange(int count);
            }

    private IEvent iEvent;

    //setter method for interface 
    public void setQuanityEvent(IEvent ievent) {
        this.lastPageHandler = handler;
    }

put this code in btnMinus.setOnClickListener

 //if ievent interface variable register via set 
  if (ievent != null) {
   //pValue is quality COUNT you want to send outside listview.
            ievent.onItemChange(pValue);
        }

activity code after creating adapter instance //ListAdapter1 adapter = new ListAdapter1(your params);

adapter.setQuanityEvent(new ListAdapter1.IEvent() {

                    @Override
                    public void onItemChange(int count) {

                        }
                    }
               });
Nil
  • 11
  • 3