DiddanDo
DiddanDo

Reputation: 401

How can I delete every item in my list?

How do I remove everything on my listview with my clickfunction below? Now it only removes the first item in the list and not every item. This is my code:

async void OnButtonClickedRemoveEverything (object sender, EventArgs args)
    {
        theGuestListMembers ourItem = null;  //theGuestListMembers is our Class.

        foreach (theGuestListMembers c in ourEventList) {   //oureventlist = this is Our List.

                ourItem = c;
        }

// . listID is a public string that gets the personal "info" from a user (the objectID). so I guess the "ourItem.listID" is our problem because I only get 1 persons objectID i guess not everyone from the list?

        if(ourItem != null)
        {
            parseAPI.deleteTheGuestList 
            (Application.Current.Properties ["sessionToken"].ToString (), ourItem.listID);    
            ourEventList.Remove (ourItem);

            EmployeeList.ItemsSource = null;  //name of our list

            EmployeeList.ItemsSource = ourEventList; 
        }

        Navigation.PopAsync ();
    }

My database where i get info.

var getItems = await parseAPI.getOurGuestList (Application.Current.Properties ["sessionToken"].ToString (), owner);
            EmployeeList.ItemsSource = null;
            ourEventList = new List<theGuestListMembers> ();

            foreach (var currentItem in getItems["results"]) {

                ourEventList.Add (new theGuestListMembers () { 
                    listID = currentItem ["objectId"].ToString (),
                    theHeadName = currentItem ["YourName"].ToString ()

                });

        }

Upvotes: 0

Views: 534

Answers (1)

AustinWBryan
AustinWBryan

Reputation: 3326

If you simply want to remove the items from the list, you can use ourEventList.Clear() or ourEventList.RemoveAll(ourItem => ourItem != null)

If you want to do something fancier, you can use this function:

private bool removeItem(theGuestListMember ourItem)
{
    if (ourItem == null) return false;

    parseAPI.deleteTheGuestList(ourItem);
    return true;
}

And use it like this:

async private void OnButtonClickedRemoveEverything(object sender, EventArgs e) 
{
   ourEventList.RemoveAll(ourItem => removeItem(ourItem);

   EmployeeList.ItemsSource = ourEventList; 
   Navigation.PopAsync ();    
}

Upvotes: 2

Related Questions