Adding dynamic data to a ListView?

summeysummey USUniversity ✭✭

I am trying to add the data I receive to a list view dynamically. I run the Xamarin app. I send the data using the UDP client I wrote in python. I then receive it in the debug console. So all of that is working fine. I would like to take that data being displayed in the console and add it to the Listview. I would like to be able to just send data from the udp client and see it update in the list view dynamically. Below is my app.

Thank you for your time.

`
using System;
using Xamarin.Forms;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Sockets.Plugin;
using System.Diagnostics;
using System.Text;

namespace ST375UDPtester
{
public class MainPage : ContentPage
{

    ListView receivedDatalistView;


    public MainPage ()
    {

        var listenPort = 5005;   
        var receiver = new UdpSocketReceiver();

        this.Padding = new Thickness (20, Device.OnPlatform (40, 20, 20), 20, 20);

        StackLayout panel = new StackLayout {
            VerticalOptions = LayoutOptions.FillAndExpand,
            HorizontalOptions = LayoutOptions.FillAndExpand,
            Orientation = StackOrientation.Vertical,
            Spacing = 10,
        };

        receiver.MessageReceived += (sender, args) =>  
        {
            // get the remote endpoint details and convert the received data into a string
            var from = String.Format ("{0}:{1}", args.RemoteAddress, args.RemotePort);
            var data = Encoding.UTF8.GetString (args.ByteData, 0, args.ByteData.Length);

            Debug.WriteLine("{0} - {1}", from, data);

        };


        // listen for udp traffic on listenPort
        receiver.StartListeningAsync (listenPort, null);



        panel.Children.Add (receivedDatalistView = new ListView{





        });

        this.Content = panel;

    }

}

}
`

Best Answer

Answers

  • summeysummey USUniversity ✭✭

    Thank you for responding. I have been searching around and that looks like the way to go is using ObservableCollection<>. I am learning now how to use this.

    Thanks again!

  • summeysummey USUniversity ✭✭

    Got it to work by doing the following hope it helps anyone else out there.

    using System;
    using Xamarin.Forms;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using Sockets.Plugin; 
    using System.Diagnostics;
    using System.Text; 
    
    
    namespace ST375UDPtester
    {
        public class MainPage : ContentPage
        {
    
    
            ListView receivedDatalistView;
    
            string from;
            string data;
    
            public MainPage ()
            {
    
    
                var listenPort = 5005;   
                var receiver = new UdpSocketReceiver();
    
                ObservableCollection<UdpDataModel> udpList = new ObservableCollection<UdpDataModel> ();
    
                this.Padding = new Thickness (20, Device.OnPlatform (40, 20, 20), 20, 20);
    
                StackLayout panel = new StackLayout {
                    VerticalOptions = LayoutOptions.FillAndExpand,
                    HorizontalOptions = LayoutOptions.FillAndExpand,
                    Orientation = StackOrientation.Vertical,
                    Spacing = 10,
                };
    
                receiver.MessageReceived += (sender, args) =>  
                {
                    // get the remote endpoint details and convert the received data into a string
                    from = String.Format ("{0}:{1}", args.RemoteAddress, args.RemotePort);
                    data = Encoding.UTF8.GetString (args.ByteData, 0, args.ByteData.Length);
    
                    udpList.Add( new UdpDataModel { From = from, Data = data } );
    
                    Debug.WriteLine("{0} - {1}", from, data);
    
                };
    
    
                // listen for udp traffic on listenPort
                receiver.StartListeningAsync (listenPort, null);
    
    
                panel.Children.Add (receivedDatalistView = new ListView{
    
                    ItemsSource = udpList
    
                });
    
                this.Content = panel;
    
            }
    
        }
    }
    

    Datamodel

    using System;
    
    namespace ST375UDPtester
    {
        public class UdpDataModel
        {
            public string From { get; set; }
    
            public string Data { get; set; }
    
            public override string ToString ( ) 
            {
    
                return string.Format( "{0} {1}", From, Data );
            }
        }
    }
    
Sign In or Register to comment.