Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Update a Picker when getting data from DB using an API

$
0
0

I am playing around with an app to test concepts, I am creating a sports registration app that will allow the user to select a participant and then register them for a sport that falls in line with the participants age and gender. IF the person in question is not listed then they can add them. That is where my issue occurs. I get the data from the DB using an API. Then it populates a picker. My issue is that i can't seem to get the picker to refresh after they add a participant. I tried and observableCollection but could not get it to populate when using the API, (had to hard code the values) Here is my code, if you could give me a hand i would appreciate it. using xamarin 3.0.

using Newtonsoft.Json;
using SportsRegTestApp.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace SportsRegTestApp
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SelectParticipant : ContentPage
{
public string uri = "...";
List participantList = new List();
HttpResponseMessage response = new HttpResponseMessage();
private ActivityIndicator _activityIndicator = new ActivityIndicator();
HttpClient client = new HttpClient();
string lwaid;
List names = new List();

    public SelectParticipant(string id)
    {
        InitializeComponent();


        lwaid = id;
        uri += id;
        while (IsBusy == true)
        {
            var indicator = new ActivityIndicator { IsRunning = true };
        }

    }

    private async void PopulatePicker()
    {

        response = await client.GetAsync(uri);
        if (response.IsSuccessStatusCode)
        {
            participantList.Clear();
            var content = await response.Content.ReadAsStringAsync();
            participantList = JsonConvert.DeserializeObject<List<Participant>>(content);

            ParticipantNames();
            PickLoading.IsVisible = false;
            ParticipantPicker.IsVisible = true;
            Add.IsVisible = true;
        }

    }


    private void ParticipantNames()
    {
        names.Clear();
        // var index = 0;

        foreach (var person in participantList)
        {
            names.Add(person.firstname + " " + person.middleinitial + " " + person.lastname + " Age:" + ((int.Parse(DateTime.Now.ToString("yyyyMMdd")) - int.Parse(person.dateofbirth.ToString("yyyyMMdd"))) / 10000));
            //index++;
        }

        ParticipantPicker.ItemsSource = names;

    }

    public async void LoadAdd()
    {
        var AddParticipant = new AddPage(lwaid);
        await Navigation.PushModalAsync(AddParticipant, true);

    }

    public async void LoadSports()
    {
        Participant selected = new Participant();
        var i = ParticipantPicker.SelectedIndex;
        if (i >= 0)
        {
            selected = participantList[i];

            var AvailableSports = new AvailableSports(selected);
            await Navigation.PushModalAsync(AvailableSports, true);

            ErrorMsg.IsVisible = false;
            ErrorMsg.Text = "";
        }
        else
        {
            ErrorMsg.Text = "You must choose a participant...";
            ErrorMsg.IsVisible = true;
        }


    }

    protected override void OnAppearing()
    {
        PickLoading.IsVisible = true;
        ParticipantPicker.IsVisible = false;
        Add.IsVisible = false;
        PopulatePicker();
    }

}

}


Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>