简体   繁体   中英

Android ListView still contains one item after removing everything

I have a ListView in Android that contains Orders. When you click on a specific order you can choose whether to remove it or not. When the list contains >1 items, the removed item does not appear on the ListView anymore. However, when the list size is 1 and you remove the only order left, the order does get removed from the list but not from the ListView. So you can still see it on the screen, but if you try to open it an error message is shown "Can't open this order.".

When you return to the Home screen and reopen the ListView, the order is properly removed, and an empty list is shown. However, I'm not sure why this is happening. Here is some sample code:

method {
    VerkoopOrder orderToBeSaved = CurrentOrder;
    UUID CurrentID = CurrentOrder.getId();
    orderToBeSaved.setId(null);
    String Result = OrderHelper.SaveOrder(orderToBeSaved, APIKey);
    JSONObject json = new JSONObject(Result);
    String res = json.getString("nummer");
    if (Result != null) {
        Messager.showMessage(getString(R.string.Saved), getString(R.string.OrderSavedAs) + " " + res, true, this);
        DeleteCurrentOrder(APIKey, CurrentID);
        UnsavedOrdersActivity.UnsavedOrderAdapter.notifyDataSetChanged();
    }
}

public void DeleteCurrentOrder(String APIKey, UUID OrderId) {
    try {
        OrderScanPreference orderScanPreference = OrderScanPreference.GetCurrentSavedPreference(this, getString(R.string.OrderScanUserPreference));
        String finalAPIKey = APIKey;
        try {
            for (UnsavedOrderPreference unsavedOrderPreference : orderScanPreference.unsavedOrderPreferences) {
                if (unsavedOrderPreference.APIAdministrationToken.equals(finalAPIKey)) {
                    unsavedOrderPreference.UnsavedOrders.removeIf(r -> r.getId().equals(OrderId)); //Order gets removed from the list!
                }
            }
            orderScanPreference.Save(this, getString(R.string.OrderScanUserPreference));
        } catch (Throwable throwable) {
            //TODO
        }
    } catch (Exception ex) {
        Log.d("Exception: ", ex.toString());
        //TODO
    }
}

This code was written by a colleague but he left the company a few weeks ago, so I have to finish his project. Let me know if you require more information.

I placed a check to see if the list is empty or not. If it is, it reassigns the Adapter to the ListView so the list gets cleared completely.

Not a great fix, so I won't accept this as the answer yet. Only if there are no better answers soon I'll accept this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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