Solved

Updating list view with repeated API calls

Posted on 2016-08-05
4
37 Views
Last Modified: 2016-08-09
Hi Experts,

I am developing a Universal Windows Platform software with VS2015 CE. I am calling some web API through JSON and passing those data to my application.

I have one list view that displays a list of data call from various APIs. The whole list view needs to be replaced with a button click event. The list is being generated the first time when the application is loading. However the list does not seem to be refreshed with the desired button click.

Part of my Code is as follows:
-------------------------------------------

private void btnCur_Click(object sender, RoutedEventArgs e)
        {
            if (btnCur.Content.Equals("USD"))
                btnCur.Content="EUR";

            else
                btnCur.Content = "USD";

            gdContents(Convert.ToString(btnCur.Content));
        }

private async void gdContents(string un)
        {
            MyProgressRing.IsActive = true;
            MyProgressRing.Visibility = Visibility.Visible;

            //The APIData class has a function to manage the API calls and put them in a list
            Data = new ObservableCollection<APIData>();     
            await APIDataManager.PopulateAPIDataAsync(Data, un);

            MyProgressRing.IsActive = false;
            MyProgressRing.Visibility = Visibility.Collapsed;
        }

Open in new window


Regards,
Soumen
0
Comment
Question by:Soumen Roy
  • 3
4 Comments
 
LVL 8

Expert Comment

by:Paweł
ID: 41747368
your issue is most likely a threading one, by making your gdContents async but returning void your application isn't syncing with the UI thread (that maybe a bad explanation)

to fix it, set your button click as async
make your gdContents methode return a Task then at the end of your method return a null object.

 return Task.FromResult<object>(null);

private async void btnCur_Click(object sender, RoutedEventArgs e)
        {
            if (btnCur.Content.Equals("USD"))
                btnCur.Content="EUR";

            else
                btnCur.Content = "USD";

          await gdContentsAsync(Convert.ToString(btnCur.Content));
        }

private async Task<object> gdContentsAsync(string un)
        {
            MyProgressRing.IsActive = true;
            MyProgressRing.Visibility = Visibility.Visible;

            //The APIData class has a function to manage the API calls and put them in a list
            Data = new ObservableCollection<APIData>();     
            await APIDataManager.PopulateAPIDataAsync(Data, un);

            MyProgressRing.IsActive = false;
            MyProgressRing.Visibility = Visibility.Collapsed;

            return await Task.FromResult<object>(null);
        }

Open in new window


try that and let me know if it works

Ok try that
0
 
LVL 8

Expert Comment

by:Paweł
ID: 41747431
If that doesn't work, could you post your APIDataManager.PopulateAPIDataAsync method
0
 
LVL 8

Accepted Solution

by:
Paweł earned 500 total points
ID: 41747486
actually you're problem is in re-instantiating of your observable collection

Data = new ObservableCollection<APIData>();

by doing that your're essentially breaking your binding, try clearing it

if you do data.clear()

and then you get a thread marshaled error, then you have the problem that i mentioned before with an inability to communicate with your UI thread, you can hack it by using the

  await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
            () => { //  });


so if your xaml is

<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <ListView ItemsSource="{Binding MyThings, Mode=TwoWay}"/>
        <Button Content="ToFrench" Click="Button_Click"/>
    </Grid>
</Page>

Open in new window


and you're code-behind is

using System.Collections.ObjectModel;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace App1
{
    public sealed partial class MainPage : Page
    {
        public ObservableCollection<string> MyThings { get; set; }
        public MainPage()
        {
            MyThings = new ObservableCollection<string>();
            MyThings.Add("One");
            MyThings.Add("Two");
            MyThings.Add("Three");
            this.InitializeComponent();
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            await ToFrench();

        }

        async Task<object> ToFrench()
        {
            MyThings.Clear();
            await PopulateFrenchDataAsync(MyThings);
            return await Task.FromResult<object>(null);
        }

        async Task<object> PopulateFrenchDataAsync(ObservableCollection<string> Data)
        {
            await Task.Run(() =>
              
                {
                    Data.Add("un");
                    Data.Add("deux");
                    Data.Add("trois");

                });

            return await Task.FromResult<object>(null);
        }
    }
}

Open in new window


change your update logic to

  async Task<object> PopulateFrenchDataAsync(ObservableCollection<string> Data)
        {
            await Task.Run(async () =>
                await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    Data.Add("un");
                    Data.Add("deux");
                    Data.Add("trois");

                }));

            return await Task.FromResult<object>(null);
        }

Open in new window

0
 

Author Closing Comment

by:Soumen Roy
ID: 41748661
Thanks for your help. It worked nicely :-)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I recently asked a question (http://www.experts-exchange.com/Programming/Smartphones/Android/Q_28684946.html) about Computer Inventory applications for Mobile Devices.  I was specifically interested in an app I could use on my android phone.  The be…
Once again I push the limits of my phone.  An introduction to the Android Google Now Launcher.
The purpose of this video is to demonstrate how to automatically show related posts at the bottom of a blog post in WordPress. This will be demonstrated using a Windows 8 PC. Plugin “Yet Another Related Posts Plugin” will be used. Go to your…
The purpose of this video is to demonstrate how to integrate Mailchimp with Facebook. This will be demonstrated using a Windows 8 PC. Mailchimp and Facebook will be used. Log into your Mailchimp account. : Click on your name. Go to Account Setti…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now