Search code examples
c#windows-phone-7windows-phone-8windows-phonelonglistselector

How to download data and show in LongListSelector?


I am working on a news reader. I get a collection of data in JSON format and I want to show it in a LongListSelector.

I faced two problems:

I convert the JSON data to a class like this:

public class User
{
    public string Username;
    public int Id;
    public string Permalink;
    public string Uri;
    public string Permalink_url;
    public string Avatar_url;
    // .. many more

    //Empty Constructor
    public User() { }
}

Then should I convert this class to a ViewModel to show it in LongListSelector with binding? (how?) or there is a better way?

we use a view model like this, so LongListSelector notifies when something changes. should I write like this for all of the above properties one by one?

public class ItemViewModel : INotifyPropertyChanged
{
    private string _lineOne;
    public string LineOne
    {
        get
        {
            return _lineOne;
        }
        set
        {
            if (value != _lineOne)
            {
                _lineOne = value;
                NotifyPropertyChanged("LineOne");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (null != handler)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

anyway, what is the best simple clean approach for getting json data and representing it in a LongListSelector and add more items if needed?


Solution

  • Say for eg. you bind your LongListSelector's ItemsSource with the ObservableCollection called items.

    <LongListSelector Name="list" ItemsSource="{Binding items}">
      .....
    </LongListSelector>
    

    Now, you need data to populate this list.User class will perform this task for you. Use Newtonsoft.Json library to get the JObject obj from json data.

    Then call the User class parameterized constructor on the screen where you want to display the data.

    User userObject = new User(obj);
    list.DataContext = new ViewModel(userObject);
    

    Define this constructor in User class as below:

    public User(JObject obj)
    {
       this.UserName = (string)obj["UserName"];
       .....
       ....
    }
    

    Now you have the UserObject which contains all the data needed to populate the LongListSelector. Use INotifyPropertyChanged on your class if you want some data to be changed later.

    public class ViewModel : INotifyPropertyChanged
    {
       ObservableCollection<UserDetail> items = new ObservableCollection<UserDetail>();
       .....
       .....
       public ViewModel(User u)
       {
         UserDetail ud = new UserDetail(u);
         //assign your data to this view class here and finally add it to the collection
         .....
         .....
    
         items.Add(ud);
       }
    }