OlayemiRondo
OlayemiRondo

Reputation: 37

how can i update my listview items whenever i update my list of object in uwp

Method for sending data to the web service

async void getData(string center)
{
    string url = "http://10.91.91.50:7500/NNRAService/webresources/NNRA/getMedicalFilter?centerName="+center;
    HttpClient client = new HttpClient();
    //string response = await client.GetStringAsync(url);
    //var data = JsonConvert.DeserializeObject(response);
    //tbOutputText.Text = data.centerName.ToString();
    //tbOutputText.Text= data.ToString();

    HttpResponseMessage response = await client.GetAsync(new Uri(url));
    var jsonString = await response.Content.ReadAsStringAsync();
    JsonArray root = JsonValue.Parse(jsonString).GetArray();
    verifyList.Clear();
    for (uint i = 0; i < root.Count; i++)
    {
        string CenterName = root.GetObjectAt(i).GetNamedString("centerName");
        string Address = root.GetObjectAt(i).GetNamedString("address");
        string status = root.GetObjectAt(i).GetNamedNumber("isActive").ToString();
        if(status=="1")
        {
            active = "SAFE";
        }

        var verified = new Class4
        {
            centerName = CenterName,
            address = Address,
            isActive = active,

        };
        verifyList.Add(verified);
    };

    verifyListView.ItemsSource = verifyList;
}

When I call the method, I want it to work in such a way that when I change the text in the textbox and I click the button, it should generate another list for me on the list view

private void btnVerify_Click(object sender, RoutedEventArgs e)
{          
    vtext = vCenter.Text;
    if(vtext != null)
    {
        getData(vtext);
    }        
}

Upvotes: 0

Views: 543

Answers (1)

Jayden
Jayden

Reputation: 3286

Do you use the List to save the Class4 Collections? If so, please set the null to ItemsSource of the ListView before you set the List to the ItemsSource of the ListView.

I suggest you use the ObservableCollection. It represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.

For example:

 private ObservableCollection<Class4> Items;
 public MainPage()
 {
     this.InitializeComponent();
     Items = new ObservableCollection<Class4>();
     verifyListView.ItemsSource = Items;
 }

By the way, if we have set the ObservableCollection to the ItemsSource of the ListView once, we do not need to set it anymore.

Upvotes: 1

Related Questions