locked
Data is not getting binded to the listview using web api in xamarin forms cross platform RRS feed

  • Question

  • User393719 posted

    Hello Sir, I'm trying to display the data from database based on dropdown selection in xamarin forms cross platform below is the code I'm using, but here nothing is getting displayed. please help me Thanks

    page3.xaml.cs

    Rostercs roles = new Rostercs(); roles.staff_name = role.SelectedItem.ToString(); string url = "http://**/api/StaffRoster/details"; HttpClient client = new HttpClient(); string JsonData = JsonConvert.SerializeObject(roles); StringContent content = new StringContent(JsonData, Encoding.UTF8, "application/json"); var response = await client.PostAsync(url,content); string result = await response.Content.ReadAsStringAsync(); var details = JsonConvert.DeserializeObject>(result); Models1.ItemsSource = details;

    ////web api public IEnumerable details( Rostercs name ) { List staffroster = new List(); SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "select * from test where rowstate=1 and staffname='"+name.staffname+"' order by date asc"; cmd.Connection = con; con.Open(); SqlDataReader staffroster = cmd.ExecuteReader(); while (staffroster.Read()) { Rostercs roster = new Rostercs(); roster.staffname = staffroster["staff_name"].ToString();

                roster.day = staff_roster["day"].ToString();
    
                staffroster.Add(roster);
            }
            con.Close();
            return staffroster;
        }
    
    Tuesday, July 28, 2020 6:07 AM

Answers

  • User371688 posted

    var response = await client.PostAsync(url,content); string result = await response.Content.ReadAsStringAsync(); var details = JsonConvert.DeserializeObject<List>(result); Models1.ItemsSource = details;

    From above code, you binded details to the ItemsSource of Models1. And the details is coming from string result = await response.Content.ReadAsStringAsync();. So , you should make sure you could get data from await response.Content.ReadAsStringAsync();.

    After that ,you can refer to my solution:

    ``` public ObservableCollection myCollection { get; set; }

    string result = await response.Content.ReadAsStringAsync();

    List details = JsonConvert.DeserializeObject>(result);

    myCollection = new ObservableCollection(details); lstView.ItemsSource = myCollection; ```

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, July 29, 2020 2:05 AM

All replies

  • User371688 posted

    Rostercs roles = new Rostercs(); roles.staff_name = role.SelectedItem.ToString(); string url = "http://**/api/StaffRoster/details"; HttpClient client = new HttpClient(); string JsonData = JsonConvert.SerializeObject(roles); StringContent content = new StringContent(JsonData, Encoding.UTF8, "application/json"); var response = await client.PostAsync(url,content); string result = await response.Content.ReadAsStringAsync(); var details = JsonConvert.DeserializeObject(result); Models1.ItemsSource = details;

    From above code you posted, we found that you directly assign the data list to the ItemsSource of your listview(Models1).

    Gerenally, we usually define ObservableCollection<T> for our listview's ItemsSource . Once the binded data is updated, then the UI will refresh automatically.

    For example, you can convert the details to ObservableCollection<MyViewModel> and assign it to Models1.ItemsSource.

    We can define a ObservableCollection to store the binded datalist and suppose our model is MyViewModel. Then we can do like this:

    public ObservableCollection<MyViewModel> myCollection { get; set; }
    
    string result = await response.Content.ReadAsStringAsync();
    
    List<VeggieViewModel> details = JsonConvert.DeserializeObject<List<MyViewModel>>(result);
    
    myCollection = new ObservableCollection<MyViewModel>(details);
    lstView.ItemsSource = myCollection;
    
    Tuesday, July 28, 2020 9:03 AM
  • User384560 posted

    Don't you need a type here?

    var details = JsonConvert.DeserializeObject<List>(result);
    

    so:

    var details = JsonConvert.DeserializeObject<List<MyObject>>(result);
    

    What are the property names you're binding to on Models1?

    Tuesday, July 28, 2020 9:07 AM
  • User393719 posted

    @ledragon , public class staffroster { public string staffname { get; set; }

    public override string ToString()
    {
            return  this.staff_name;
    }
    }
    

    I'm trying to filter the listview data based on picker selection and display the same in listview.Please help.Thanks

    Tuesday, July 28, 2020 9:46 AM
  • User384560 posted

    Create a type with the same property name in your Xamarin app, then as jexh mentions cast to an ObservableCollection.

    To filter Listview create a property (e.g. selectedStaffName) on your viewmodel and bind

    SelectedItem="{Binding selectedStaffName}"
    

    ...on the Picker to this property.

    Then you can use Behaviours to bind "Unfocused" event to a command on the viewmodel to filter the Listview ItemSource.

    Tuesday, July 28, 2020 10:09 AM
  • User393719 posted

    @ledragon , please can you share a sample example for filtering in listview .Please help me.Thanks

    Tuesday, July 28, 2020 10:27 AM
  • User384560 posted

    Took me 3 seconds to find this:

    https://montemagno.com/xamarin-forms-filtering-a-listview-with-a-bindable-picker/

    Tuesday, July 28, 2020 10:31 AM
  • User393719 posted

    @ledragon , I've gone through the given link, but in my example i am using web api. unable to bind it using picker selection.Please help me thanks

    Tuesday, July 28, 2020 10:41 AM
  • User384560 posted

    You've been given everything you need in this post.

    Have you implemented jexh solution:

    public ObservableCollection<MyViewModel> myCollection { get; set; }
    
    string result = await response.Content.ReadAsStringAsync();
    
    List<VeggieViewModel> details = JsonConvert.DeserializeObject<List<MyViewModel>>(result);
    
    myCollection = new ObservableCollection<MyViewModel>(details);
    lstView.ItemsSource = myCollection;
    

    If so, does the details property contain your API data?

    Tuesday, July 28, 2020 11:08 AM
  • User393719 posted

    @ledragon , the web api is working correctly,please help me.thanks

    Tuesday, July 28, 2020 1:45 PM
  • User384560 posted

    That wasn't the question. Does the "details" property in "page3.xaml.cs" contain your API data?

    If not then you haven't implemented the answer as provided so far.

    Tuesday, July 28, 2020 4:17 PM
  • User371688 posted

    var response = await client.PostAsync(url,content); string result = await response.Content.ReadAsStringAsync(); var details = JsonConvert.DeserializeObject<List>(result); Models1.ItemsSource = details;

    From above code, you binded details to the ItemsSource of Models1. And the details is coming from string result = await response.Content.ReadAsStringAsync();. So , you should make sure you could get data from await response.Content.ReadAsStringAsync();.

    After that ,you can refer to my solution:

    ``` public ObservableCollection myCollection { get; set; }

    string result = await response.Content.ReadAsStringAsync();

    List details = JsonConvert.DeserializeObject>(result);

    myCollection = new ObservableCollection(details); lstView.ItemsSource = myCollection; ```

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, July 29, 2020 2:05 AM