【问题标题】:How to Pass a Value from One Activity to Another Activity/Adapter如何将值从一个活动传递到另一个活动/适配器
【发布时间】:2018-12-16 20:45:21
【问题描述】:

我正在尝试将我的Cartadapter 的值传递给CartActivity

我的代码如下:

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 函数:

 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);
    }

我的问题是 cartTotalChanged((cartAdapter.getTotal())); 返回一个 Null 并且我的应用程序因多线程故障而崩溃。

请帮忙

【问题讨论】:

  • 欢迎来到 Stack Overflow!看起来您可能需要学习使用调试器。请帮助自己一些complementary debugging techniques。如果之后您仍有问题,请edit您的问题更具体地说明您需要什么帮助。
  • 你能显示堆栈跟踪吗?

标签: java android android-studio android-intent


【解决方案1】:

要将数据从适配器传递到活动,请使用以下方式:

1.使用Interface创建新的接口类。

2.在活动中实现该接口类并覆盖传递数据方法。

3.在适配器类中将变量作为内部参数分配给接口方法

接口类:

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

适配器类:

  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");

活动类:

Class MainActivity implements Ainterface
{

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

我希望它对你有用

【讨论】:

    【解决方案2】:

    你可以像下面这样创建一个类:

    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;
    }
    

    }

    然后在你的适配器上这样设置:

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

    并在您的活动中收到:

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

    您可以根据需要设置多个像这样的全局变量。

    希望对你有帮助!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-22
      • 2010-11-10
      • 2021-08-17
      • 2011-10-27
      相关资源
      最近更新 更多