0

我正在 android 中创建一个演示购物车,为此我正在使用 Application 类来保存数据。我无法从链表中删除数据。我正在为 android 活动调用 removeItem() 函数以从列表中删除选定的项目,但它不起作用任何人都可以帮助我。

    package in.co.santoshsharma.bookshopping;

import java.util.LinkedList;

import android.app.Application;
import android.content.res.Configuration;

public class GlobalData extends Application{
    private String email;
    private String itemName;
    private int itemQuantity;
    private int itemCost;

    public GlobalData(){

    }
    public  GlobalData(String iName,int iQunt,int iCost) {
        // TODO Auto-generated constructor stub
        this.itemCost=iCost;
        this.itemName=iName;
        this.itemQuantity=iQunt;

    }




    public void setEmail(String mail)
    {
        this.email=mail;
    }

    public String getEmail()
    {
        return email;
    }
    public String getItemName()
    {
        return itemName;
    }

    public int getItemCost()
    {
        return itemCost;
    }

    public int getItemQunt()
    {
        return itemQuantity;
    }



    LinkedList<GlobalData> list = new LinkedList<GlobalData>();

    public void setList(String iName,int iQunt,int iCost)
    {
        list.add(new GlobalData( iName, iQunt, iCost));
    }

    public LinkedList<GlobalData> getList()
    {
        return list;
    }
    public void removeItem(String iName,int iQunt,int iCost)
    {
        for(GlobalData data:list)
        {
            if(data.getItemName().equals(iName))
            {
                list.remove(itemName);
                //list.remove(iCost);
                //list.remove(iQunt);
            }
        }
    } 



}
4

5 回答 5

1

First, override equals() method and use itemName attribute for the comparison

@Override
public boolean equals(Object o) {
  if (o == null) return false;
  if (itemName == null) return false;
  if (o instanceOf String) return itemName.equals(o);
  else if (o instanceOf GlobalData) return ((GlobalData) o).itemName.equals(this.itemName);
  else return false;
}

Then, change your removeItem() method

public void removeItem(String iName) {
  list.remove(iName);
  // or uncomment line below to completely remove all matching elements
  // for (;;list.remove(iName)) {}
}

According to http://docs.oracle.com/javase/6/docs/api/java/util/LinkedList.html#remove(java.lang.Object) remove() method of a LinkedList will call the equals() method of the supplied Object and compare it with every element in the list.

Hope this helps :)

于 2012-10-27T12:59:02.913 回答
0

当您对它们进行迭代时,您不能在列表中操作(添加、删除...项目)。您必须使用迭代器

for(Iterator<EmpDedup> iter = list.iterator(); iter.hasNext();) {
    EmpDedup data = iter.next();
    if (data.getRecord() == rec1) {
        iter.remove();
    }
}

请参阅http://docs.oracle.com/javase/6/docs/api/java/util/Iterator.html

引用自https://stackoverflow.com/a/10735435/1602230

于 2012-10-27T11:17:45.330 回答
0
于 2012-10-27T12:03:31.577 回答
0

使用迭代器删除元素:

public void removeItem(String iName, int iQunt, int iCost) {
  Iterator<GlobalData> iterator = list.iterator();
  while (iterator.hasNext()) {
    GlobalData data = iterator.next();
    if (data.getItemName().equals(iName)) {
      iterator.remove();
    }
  }
}
于 2012-10-27T12:12:15.353 回答
0

-您正在同时访问和修改Collection, 不能For-Each直接从循环中完成..

-用来Iterator解决这个问题。

LinkedList<GlobalData> q1 = new LinkedList<GlobalData>();

Iterator<GlobalData> iterator = q1.iterator();

while (iterator.hasNext()){

         GlobalData mp = iterator.next();

         if (mp.name.equals("xyz")){

             iterator.remove();    // You can do the modification here.
         }


 }
于 2012-10-27T12:14:08.360 回答