Nagikho Nagikho - 5 months ago 26
Java Question

Toast Message does not disappear

I have a for loop and I'm doing some searching stuff in it. I want to show toast message when process is unsuccessful. I can show toast message but it does not disappear when I want to search again.

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;
}

Answer

Removing Toast from for loop might help!

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;
}