Problem to update ListView whit ObservableCollection

Miki93Miki93 DOMember ✭✭

Well First than nothing, i don't speak english, sorry if i write anything wrong.

My probles is the next, i working with Websocket, my server send to my app a list of Json, that is fine, but my problem is when i try show this list of json y my ListView across of my ObservableCollection, if i initialize all in the constructor of my ViewModel before, the View show the items rigth, but when i try to update that lisview whit the data of the Json, this don't update, here is my code:

This is the MODEL, to receive the correct format of the JSON

       public class datosJson : INotifyPropertyChanged{
            #region VARIABLES
            private String _id;
            private String _nombre;
            private String _descripcion;
            #endregion

            #region PROPIEDADES
            public string Id { get => _id;
                set {
                    _id = value;
                    OnPropertyChanged();
                }
            }
            public string Nombre { get => _nombre;
                set {
                    _nombre = value;
                    OnPropertyChanged();
                }
            }
            public string Descripcion { get => _descripcion;
                set {
                    _descripcion = value;
                    OnPropertyChanged();
                }
            }
            #endregion

            #region INotityPropertyChanged
            public event PropertyChangedEventHandler PropertyChanged;

            void OnPropertyChanged([CallerMemberName] string propertyName = null){
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
            #endregion
}

This is my View with my ListView component, 'Datos' is my ObservableCollections and 'Nombre' 'Descripcion' the fields than i want show:~~~~

        <ListView ItemsSource="{Binding Datos}"
                  Header="JSON"
                  Footer="Fin del listado">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout>
                            <Label Text="{Binding Nombre}"/>
                            <Label Text="{Binding Descripcion}"/>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

This is my ViewModel:

      class ChatPageViewModel : INotifyPropertyChanged {
     List<datosJson> orders = new List<datosJson>();
         public ObservableCollection<datosJson> Datos { get; set; }

      #region CONSTRUCTOR
          public ChatPageViewModel() {
          Datos = new ObservableCollection<datosJson>(orders);
          }

      #region METODOS
          public async void ConnectToServerAsync() {
      <CodeToConnectWithServer>
       ...
       orders = JsonConvert.DeserializeObject<List<datosJson>>(serialisedMessage);

           //HERE IS MY PROBLEM                      
           Datos = new ObservableCollection<datosJson>(orders.ToList());

           //SEE IF THE DATA IS COMING RIGTH
           for(int i = 0; i < Datos.Count; i++){
                      Console.WriteLine("-MENSAGE:- " + orders[i].Nombre);
           }
       }
       #endregion

I don't knows why the listview don't update if i set the data in my ObservableCollection..

PLEASE HELP ME :(

Best Answer

  • AlessandroCaliaroAlessandroCaliaro IT ✭✭✭✭✭
    Accepted Answer
    You can try to do

    Datos.Clear();

    foreach(datosJson order in orders){

    Datos.Add(order);
    }

    I am writing with my mobile... sintax maybe wrong...

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    Accepted Answer
    You can try to do

    Datos.Clear();

    foreach(datosJson order in orders){

    Datos.Add(order);
    }

    I am writing with my mobile... sintax maybe wrong...
  • Miki93Miki93 DOMember ✭✭

    @AlessandroCaliaro said:
    You can try to do

    Datos.Clear();

    foreach(datosJson order in orders){

    Datos.Add(order);
    }

    I am writing with my mobile... sintax maybe wrong...

    oh god, that's work fine, VERY THANKS!!! :dizzy:

Sign In or Register to comment.