哈希映射返回空值问题



>我有两个模型,第一个是cartItem模型,具有HashMap <String,ProductOption>,ProductOption是第二个模型。

第一个模型 :

public class CartItem extends BaseObservable implements Serializable {
...
HashMap<String, ProductOption> options;
...
@Ignore
public HashMap<String, ProductOption> getOptions() {
return options;
}
@Ignore
public void setOptions(HashMap<String, ProductOption> options) {
this.options = options;
}
}

第二种型号:

public class ProductOption implements Parcelable {
String optionId;
String optionKey;
String optionValue;}

当我尝试将购物车项目列表设置为回收器视图适配器时,所有购物车项目模型项目都会复制,但选项复制为空

mViewModel.getCartItems().observe(this,cartItems -> {
if (FirebaseAuth.getInstance().getCurrentUser() == null) return;
if (cartItems ==null ) return;

cartItemList = cartItems;
cartAdapter.setCartItemList(cartItems);

});

谢谢你的帮助。

我发现使选项为空的原因是我通过迭代器循环选项变量并使用它.remove((这种循环删除选项的方式 cartItemList

if (cartItemList.get(i).getOptions() != null){
HashMap<String, ProductOption> optionHashMap = cartItemList.get(i).getOptions();
Iterator it = optionHashMap.entrySet().iterator();
double optionPrice = 0;
while (it.hasNext()){
Map.Entry pair = (Map.Entry) it.next();
optionPrice += Double.valueOf(((ProductOption)pair.getValue()).getOptionValue());
it.remove();
}
price = price + optionPrice;
}

最新更新