0

I have an activity from which I created a dialog passing a list of objects. In the dialog, I have modified the global object list data.

Problem is my activity object list is also modified from the dialog data manipulation. I didn't use any listener or method to do caller class data. AFAIK these are a separate object and doesn't have the same reference, So it shouldn't have changed.

Activity caller section:

ProductQuantityReviewDialog dialog = new ProductQuantityReviewDialog(context, salesOrderList);
dialog.show();

Dialog class :

private HashMap<Integer, OrderSalesModel> salesOrderList;

public ProductQuantityReviewDialog(@NonNull Context context, HashMap<Integer, OrderSalesModel> salesOrderList) {
        super(context);
        this.context = context;
        this.salesOrderList = salesOrderList;
    }

Update: Tried in this way but same problem.

    public ProductQuantityReviewDialog(@NonNull Context context, HashMap<Integer, OrderSalesModel> salesOrderList) {
        super(context);
        this.context = context;
        orderList = new HashMap<>(salesOrderList);
       // orderList.putAll(salesOrderList);
}
Sadiq Md Asif
  • 882
  • 6
  • 18

2 Answers2

1

Passing reference inside constructor makes dialog hash map refer to same address as the activity hash map.Instead of passing reference you should maintain a separate list inside dialog.

private Map<Integer, OrderSalesModel> mSalesOrderList;

public RetrieveInput(@NonNull Context context, HashMap<Integer, OrderSalesModel> salesOrderList) {
    super(context);
    this.context = context;
    copyDataToMap(salesOrderList);
}

private void copyDataToMap(HashMap<Integer, OrderSalesModel> salesOrderList) {
    mSalesOrderList = new HashMap<>();
    for (Map.Entry<Integer, OrderSalesModel> entry : salesOrderList.entrySet())
    {
        mSalesOrderList.put(entry.getKey(), entry.getValue());
    }
}
Sharath kumar
  • 4,064
  • 1
  • 14
  • 20
0

In your code . you are passing reference of the hashmap . so another hashmap is also alter .

Initialize new hashmap in you dialog and set all the value to it .

Tejas Pandya
  • 3,987
  • 1
  • 26
  • 51