简体   繁体   中英

How to Pass a Value from One Activity to Another Activity/Adapter

Iam trying to pass a value my Cartadapter to CartActivity .

My code is as follows:

CartActivity.java:

    cartTotalChanged((cartAdapter.getTotal()));

CartAdapter.java:

    public Double getTotal() {
        Double total = 0d;
        try{
            for (MenuItem item : dataList)
                total += item.getTotal();
        }
        catch(Exception e){
            return total;
        }
        finally{
            return total;
        }


    }

cartTotalChanged Function:

 public void cartTotalChanged(Double totalAmount) {
        if (coupon != null) {
            totalAmount = totalAmount - (coupon.getType().equals("fixed") ? coupon.getReward() : ((totalAmount * coupon.getReward()) / 100));
        }
        DecimalFormat decimalFormat = new DecimalFormat("###.##");
        subtotal.setText(decimalFormat.format(totalAmount) + currency);
        double sc = (totalAmount * serviceCharge / 100);
        feeService.setText(decimalFormat.format(sc) + currency);
        total.setText(decimalFormat.format(totalAmount > 0 ? (totalAmount + sc + deliveryFee) : 0) + currency);
        Helper.setCart(sharedPreferenceUtil, cartItems);
    }

My problem here is cartTotalChanged((cartAdapter.getTotal())); is returning a Null and My app crashed with multiple thread failure.

kindly help

To pass data from adapter to activity use following way:

1.use Interface to create new interface class.

2.In activity implement that interface class and override passing data method.

3.In adapter class assign variable as inside parameter to interface method

Interface class:

Class Ainterface
{ 
public void passData(String setData);
 }

Adapterclass:

  Ainterface ainterface;
  Adapterclass(Context context)
    {
     ainterface=context;
    }

    /*add below line in your onbind or onclick... whenever you want to pass data from adpter to activity use below line*/
    ainterface.passData("set your variable which is load to activity");

Activity class:

Class MainActivity implements Ainterface
{

/*inside onCreate */
AdapterClass ac=new AdapterClass(this);
public void passData(String getdata)
{
Log.v("String is",getdata);
/*do something*/
}
}

i hope its work on you

You can create a class like below:

public class Globals{
private static Globals instance;

// Global variable

private Double cartTotal;


// Restrict the constructor from being instantiated
private Globals(){}

public void setData(Double d){
    this.cartTotal=d;
}
public Double getData(){
    return this.cartTotal;
}

public static synchronized Globals getInstance(){
    if(instance==null){
        instance=new Globals();
    }
    return instance;
}

}

Then on your adapter set it like this:

Globals g = Globals.getInstance();
g.setData(cartTotal); 

And receive in your Activity as:

Globals g = Globals.getInstance();
Double cartTotal = g.getData();

You can set a number of Global variables like this as per your requirement.

Hope this helps !!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM