简体   繁体   English

如何从列表<>中删除项目?

[英]How to remove an item from List<>?

I'm developing an app, which contains a list of tickets, there are add ticket and remove ticket options for the user. 我正在开发一个应用程序,其中包含票证列表,为用户提供了添加票证和删除票证选项。 The add option works fine, but the remove option doesn't, here is the ticket list code 添加选项可以正常工作,但删除选项却不能,这是故障单列表代码

public class TicketList {

    private List<Ticket> ticketList;

    public TicketList() {
        ticketList = new ArrayList<>();
    }

    public List<Ticket> getTicketList() {
        return ticketList;
    }

    public void setTicketList(List<Ticket> ticketList) {
        this.ticketList = ticketList;
    }

    public void addTicketToList(Ticket ticket) {
        ticketList.add(ticket);
    }

    public void removeFromList(Ticket ticket) {
        ticketList.remove(ticket);
    }

    @Override
    public String toString() {
        return "TicketList{" + "ticketList=" + ticketList + '}';
    }
}

The delete function in another activity wich doesn't work: 在另一个活动中的删除功能不起作用:

private void deleteTicket() {
    TicketList ticketList = MyPreferencesManager.getInstance(this).getTicketList();
    Ticket ticket = MyPreferencesManager.getInstance(this).getTicket();
    ticketList.removeFromList(ticket);
    MyPreferencesManager.getInstance(this).putTicketList(ticketList);
}

While the add function works fine: 尽管add函数可以正常工作:

private void saveTicket() {
    TicketList ticketList = MyPreferencesManager.getInstance(this).getTicketList();
    Ticket ticket = new Ticket();
    ticket.setUsername(username.getText().toString());
    ticket.setPassword(password.getText().toString());
    ticketList.addTicketToList(ticket);
    MyPreferencesManager.getInstance(this).putTicketList(ticketList);
}

Can anyone tell me what's wrong with the deleting or removing function? 谁能告诉我删除或删除功能出了什么问题?

It is because when you're using the following code: 这是因为当您使用以下代码时:

ticketList.remove(ticket);

the remove method will check if there is an exact object item in the list before removing. remove方法将在删除之前检查列表中是否有确切的对象项 So, your ticket object probably is already changed before you're trying to delete it from the list. 因此,在尝试从列表中删除票证对象之前,它可能已更改。

You can see the details from List documentation : 您可以从列表文档中查看详细信息:

public abstract boolean remove (Object o) 公共抽象布尔移除(对象o)

Removes the first occurrence of the specified element from this list, if it is present (optional operation). 如果存在指定元素,则从列表中删除该元素的第一次出现(可选操作)。 If this list does not contain the element, it is unchanged. 如果此列表不包含该元素,则它保持不变。 More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists). 更正式地讲,删除索引i最低的元素,使得(o == null?get(i)== null:o.equals(get(i)))(如果存在这样的元素)。 Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call). 如果此列表包含指定的元素(或者等效地,如果此列表由于调用而更改),则返回true。

What you need is probably List.remove(int index) . 您可能需要的是List.remove(int index)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM