简体   繁体   中英

Xamarin forms ListView keep populating data from List<object> instead of simply display the objet once

I keep encountering this problem no matter what :

The List<Object> listArticles is simply keep adding data everytime the page appears ,it keep populating the data over and over again instead of simply display them once

I have tried to declare a tempList everytime the function is called , but no success List<PlanDefinition> tempListArticles = new List<PlanDefinition>(listArticles)

How to encouter this problem?

async protected override void OnAppearing()
        {

            listArticles= articleView.getArticlesFromPlan();    
            PopulateOrderLists(listArticles);

            base.OnAppearing();
        }

        protected override void OnDisappearing()
        {

            base.OnDisappearing();
        }






  private void PopulateOrderLists(List<PlanDefinition> listArticles)
            {
                List<PlanDefinition> tempListArticles = new List<PlanDefinition>(listArticles);
                OrderTemplate prod = new OrderTemplate();



            for (var i = 0; i < tempListArticles.Count; i++)
            {
                  prod = new OrderTemplate();

                prod.BindingContext = tempListArticles[i].Name;
                 OrderInformation.Children.Add(prod);

             }

        }

Only call your PopulateOrderLists once, assumably when the listArticles has not been populated, but adjust it to your requirements of when you want to replace the current listArticles or add to it...

Example:

async protected override void OnAppearing()
{
    if (listArticles != null && listArticles.Count > 0)
    {
       listArticles= articleView.getArticlesFromPlan();    
       PopulateOrderLists(listArticles);
    }
    base.OnAppearing();
}

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