gpt4 book ai didi

java - Arraylist 覆盖值问题?

转载 作者:行者123 更新时间:2023-11-30 02:42:19 24 4
gpt4 key购买 nike

我有一个 Order Items 对象。它包含三个值

1.项目名称

2.商品价格

3.元素数量

我有一个微调器,我已将此对象添加到所选项目上。

当我添加这个对象并再次选择时,另一个项目也会添加到列表中,而不是覆盖。

我的代码如下

    private OnItemSelectedListener itemSelectListener = new OnItemSelectedListener() {


@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
// TODO Auto-generated method stub


System.out.println("parent Tag:"+arg0.getTag());
TextView itemName = (TextView)findViewById(Integer.parseInt(arg0.getTag().toString()));
System.out.println("Item Name:" +itemName.getText().toString());
System.out.println("Item Price:" +itemName.getTag().toString());
System.out.println("Item QTY:" +arg0.getItemAtPosition(arg2).toString());
orderItems = new OrderItems();
if(!"".equals(arg0.getItemAtPosition(arg2).toString())){
orderItems.itemName = itemName.getText().toString();
orderItems.itemPrice = itemName.getTag().toString();
orderItems.noOfItems = Integer.parseInt(arg0.getItemAtPosition(arg2).toString());
//if(itemsArray)
itemsArray.additem(orderItems);
}

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
};

public void additem(OrderItems object){
itemArrayList.add(object);
}

任何人都可以帮助我吗?我在这里缺少什么?

最佳答案

尝试这种方式,希望这能帮助您解决问题。

HashMap<Integer,Object> hashMap = new HashMap<Integer, Object>();
private OnItemSelectedListener itemSelectListener = new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
System.out.println("parent Tag:"+arg0.getTag());
TextView itemName = (TextView)findViewById(Integer.parseInt(arg0.getTag().toString()));
System.out.println("Item Name:" +itemName.getText().toString());
System.out.println("Item Price:" +itemName.getTag().toString());
System.out.println("Item QTY:" +arg0.getItemAtPosition(arg2).toString());
orderItems = new OrderItems();
if(!"".equals(arg0.getItemAtPosition(arg2).toString())){
orderItems.itemName = itemName.getText().toString();
orderItems.itemPrice = itemName.getTag().toString();
orderItems.noOfItems = Integer.parseInt(arg0.getItemAtPosition(arg2).toString());
hashMap.put(arg2,orderItems);
}
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {

}
};

关于java - Arraylist 覆盖值问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25602485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com