1

I have created a Custom Listener interface for Button click in Adapter class, i followed this tutorial: http://www.c-sharpcorner.com/UploadFile/9e8439/create-custom-listener-on-button-in-listitem-listview-in-a/

Adapter:

        holder.btnQtyIncrease.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                 if (customListner != null) {  
                        customListner.onButtonClickListner(position);  
                 }  

                cart = cartArrayList.get(position);
                holder.textViewQuantity.setTag(cart);

                if(cart.getQuantity() == 9) {
                    Toast.makeText(context, "Already reached", Toast.LENGTH_LONG).show();
                    return ;
                }
                else 
                {                                                       
                    cart.setQuantity(cart.getQuantity() + 1);
                    holder.textViewQuantity.setText(String.valueOf(cart.getQuantity()));

                    totalPrice = cart.getQuantity() * cart.getPrice();

                    CartArrayList.cartArraylist.get(position).setTotal(totalPrice);
                    holder.textViewTotal.setText(cart.getTotal());
                }

            }
        });

        .....       
        return convertView;
    }

And I have implemented Listener in Activity like this:

public class CartActivity extends Activity implements customButtonListener {    
    ......

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("CartActivity-onCreate", "onCreate");

        setContentView(R.layout.activity_cart);     

        ......              

    }   


    @Override
    public void onResume()
    {
        super.onResume();   
        Log.d("CartActivity-onResume", "onResume");             
    }

    @Override
    public void onButtonClickListner(int position) {

        totalPrice = CartArrayList.cartArraylist.get(position).getQuantity() * CartArrayList.cartArraylist.get(position).getPrice();   
        CartArrayList.cartArraylist.get(position).setTotal(totalPrice);

        subTotal = subTotal + totalPrice;
    }

}

As you can see inside for loop i am setting and getting total of each and every list item, and then calculating subTotal of ArrayList items...

But whenever i do tap on btnQtyIncrease it makes change in total of list item price, but it would not effect on subTotal amount

Sun
  • 6,768
  • 25
  • 76
  • 131
  • I am thinking that you are using view holder pattern for popping list. So i n your code before setting onclicklistener holder.btnQtyIncrease.setOnClickListener( you should check if holder is set or not means if(holder != null) otherwise you will be setting new clicklistener each time, you should set click listener only first time. If I missed anything post your source code and problem in detail. – Prashant May 15 '15 at 09:17
  • try putting ` customListner.onButtonClickListner(position);` at the end of click code – SweetWisher ツ May 15 '15 at 12:28

2 Answers2

2

You should change you logic in some way that it calculates the subTotal in adapter only and then just pass the final value to interface which will simply set it in then textview

So your code should look like:

holder.btnQtyIncrease.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            cart = cartArrayList.get(position);
            holder.textViewQuantity.setTag(cart);

            if(cart.getQuantity() == 9) {
                Toast.makeText(context, "Already reached", Toast.LENGTH_LONG).show();
                return ;
            }
            else 
            {                                                       
                cart.setQuantity(cart.getQuantity() + 1);
                holder.textViewQuantity.setText(String.valueOf(cart.getQuantity()));

                totalPrice = cart.getQuantity() * cart.getPrice();

                CartArrayList.cartArraylist.get(position).setTotal(totalPrice);
                holder.textViewTotal.setText(cart.getTotal());
            }

        }
        // calculate total here
         if (customListner != null) {  
                customListner.onButtonClickListner(getTotalValue());  
          }  

    });

Define a method getTotalValue in adapter which will iterate through the array and find the subTotal value.

And then simply set it in the textView

@Override
public void onButtonClickListner(float subTotal ) {
        txtView.setText(String.valueOf(subTotal));
}

Accordingly, change the interface signature as :

public void onButtonClickListner(float total);
SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
0

How to update value in Activity when do change in Adapter class

To update data in Activity on btnQtyIncrease Button click. create custom event listener using interface which will trigger in Activity when any change made in Quantity.

See following example for creating custom Listener :

How to create our own Listener interface in android?

Community
  • 1
  • 1
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • I went to link, but i did not get exact usage of Custom Interface.. would you like to tell me the usage of Interface and how can i use this in existing situation, you may find more code here: http://stackoverflow.com/questions/30166412/how-to-call-adapter-class-before-activity – Sun May 15 '15 at 09:44
  • @Sun: you want to change `subTotal `accoding to total changes on `btnQtyIncrease` Button Click? – ρяσѕρєя K May 15 '15 at 09:46
  • @Sun: ok then use custom Interface as in provided link to get total change on button click in Activity then update subTotal – ρяσѕρєя K May 15 '15 at 09:50
  • i am not getting exactly what to do ? How can we use interface to get that ? – Sun May 15 '15 at 09:51
  • @Sun: dude do it in same way as in provided example in link – ρяσѕρєя K May 15 '15 at 09:52
  • like i told you I did not understand the usage of Interface, that' why i don't know How do I use that ? show me the way ...for my further learning – Sun May 15 '15 at 09:54
  • @Sun: create `MyListener` interface and `implements` in Activity . send `this` to Adapter class and call ` ml.callback(this, ""+cart.getTotal());` on `btnQtyIncrease` button click by passing value – ρяσѕρєя K May 15 '15 at 09:55