Java 祝酒词不会消失

Java 祝酒词不会消失,java,android,for-loop,toast,android-toast,Java,Android,For Loop,Toast,Android Toast,我有一个for循环,我正在里面搜索一些东西。我想在进程失败时显示toast消息。我可以显示toast消息,但当我想再次搜索时,它不会消失 public String SearchInstallationBySerialNumber(String serial){ String installation = null; for(int i = 0; i < _allItems.size(); i++){ OsbDownloadItem currentOsbIt

我有一个for循环,我正在里面搜索一些东西。我想在进程失败时显示toast消息。我可以显示toast消息,但当我想再次搜索时,它不会消失

public String SearchInstallationBySerialNumber(String serial){
    String installation = null;
    for(int i = 0; i < _allItems.size(); i++){
        OsbDownloadItem currentOsbItem = _allItems.get(i);
        if(!currentOsbItem.getSerialNumber().equals(serial)){
            Toast.makeText(mActivity,
                           "unsuccessful searching",
                           Toast.LENGTH_SHORT).show();
            continue;
        }else{
            installation = currentOsbItem.getInstallation();
        }
    }
    return installation;
}
public String SearchInstallationBySerialNumber(字符串序列){
字符串安装=null;
对于(int i=0;i<_allItems.size();i++){
OsbDownloadItem currentOsbItem=_allItems.get(i);
如果(!currentOsbItem.getSerialNumber().equals(串行)){
Toast.makeText(mActivity,
“搜索不成功”,
吐司。长度(短)。show();
继续;
}否则{
installation=currentOsbItem.getInstallation();
}
}
返回安装;
}

从for循环中删除Toast可能会有所帮助

public String SearchInstallationBySerialNumber(String serial) {

    String installation = null;
    for (int i = 0; i < _allItems.size(); i++) {

        OsbDownloadItem currentOsbItem = _allItems.get(i);
        if (currentOsbItem.getSerialNumber().equals(serial)) {

            installation = currentOsbItem.getInstallation();
            break;
        }

    }
    if (installation == null) {
        Toast.makeText(mActivity, "unsuccessful searching", Toast.LENGTH_SHORT).show();
    }
    return installation;
}
public String SearchInstallationBySerialNumber(字符串序列){
字符串安装=null;
对于(int i=0;i<_allItems.size();i++){
OsbDownloadItem currentOsbItem=_allItems.get(i);
如果(currentOsbItem.getSerialNumber().equals(串行)){
installation=currentOsbItem.getInstallation();
打破
}
}
如果(安装==null){
Toast.makeText(mActivity,“搜索不成功”,Toast.LENGTH_SHORT.show();
}
返回安装;
}

您可能在循环中再次调用此
Toast.makeText
<代码>继续跳转到下一个迭代,对吗?如果要隐藏任何旧的toast,可能应该保存toast并调用toast.cancel()。我无法调用toast.cancel()