Forum Xamarin.Forms

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

DemoCloudDemoCloud Member ✭✭✭

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<List>(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 staff_name='"+name.staff_name+"' order by date asc";
cmd.Connection = con;
con.Open();
SqlDataReader staff_roster = cmd.ExecuteReader();
while (staff_roster.Read())
{
Rostercs roster = new Rostercs();
roster.staff_name = staff_roster["staff_name"].ToString();

            roster.day = staff_roster["day"].ToString();

            staffroster.Add(roster);
        }
        con.Close();
        return staffroster;
    }

Best Answer

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    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;
    
  • ledragonledragon Member ✭✭✭
    edited July 2020

    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?

  • DemoCloudDemoCloud Member ✭✭✭
    edited July 2020

    @ledragon ,
    public class staff_roster
    {
    public string staff_name { 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

  • ledragonledragon Member ✭✭✭
    edited July 2020

    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.

  • DemoCloudDemoCloud Member ✭✭✭

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

  • DemoCloudDemoCloud Member ✭✭✭

    @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

  • ledragonledragon Member ✭✭✭

    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?

  • DemoCloudDemoCloud Member ✭✭✭

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

  • ledragonledragon Member ✭✭✭
    edited July 2020

    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.

Sign In or Register to comment.