Search code examples
listviewxamarin.formspicker

Xamarin forms: How to get the selected picker model data inside a listview?


I have a picker in my Listview. I am binding data to the listview from a REST API call. Following is my model:

public class Attendance
{
  public List<cbrainAttendanceHBList> cbrainAttendanceHBList { get; set; }
}

public class cbrainAttendanceHBList
{
   public string userId { get; set; }
   public string name { get; set; }
   public string isPresent { get; set; } 
   public string status
    {
        get
        {
            if (isPresent == "1.0")
                return "Present";
            else if (isPresent == "0.0")
                return "Absent";
            else if (isPresent == "0.5")
                return "Half Day";
            else return "";
        }
    }
}

Sample Json:

{  
   "cbrainAttendanceHBList":[  
      {  
         "userId":11709,
         "name":"zzzz",
         "isPresent":0.0,
         "date":1561637200408
      },
      {  
         "userId":11702,
         "name":"xxxx",
         "isPresent":1.0,
         "date":1561637200408
      },
      {  
         "userId":11699,
         "name":"yyyy",
         "isPresent":0.5,
         "date":1561637200408
      }
   ]
}

Picker is inside of the ListView. Picker code:

 <ListView 
       x:Name="StudentList"
       RowHeight="75"
       BackgroundColor="White"
       HasUnevenRows="True">  
       <ListView.ItemTemplate>
           <DataTemplate>
                 <ViewCell>
                       <ViewCell.View>
                            <StackLayout 
                                  HorizontalOptions="FillAndExpand"
                                  VerticalOptions="FillAndExpand"
                                  Margin="5"
                                  Padding="5"
                                  Orientation="Horizontal">

                                  <Label 
                                        Text="{Binding name}"
                                        Font="17" 
                                        TextColor="#474747"
                                        HorizontalOptions="Start" 
                                         VerticalOptions="Center"/>

                                   <Picker 
                                         HorizontalOptions="EndAndExpand"
                                         Margin="0,0,20,0"
                                         SelectedItem="{Binding status}"
                                         SelectedIndexChanged="AttendanceStatus"
                                         WidthRequest="100"
                                         VerticalOptions="CenterAndExpand"
                                         TextColor="#5abcd7"  
                                         HeightRequest="50">
                                         <Picker.ItemsSource>
                                              <x:Array Type="{x:Type x:String}">
                                                  <x:String>Present</x:String>
                                                  <x:String>Half Day</x:String>
                                                  <x:String>Absent</x:String>
                                                </x:Array>
                                           </Picker.ItemsSource>
                                     </Picker>
                             </StackLayout>
                         </ViewCell.View>
                     </ViewCell>
                  </DataTemplate>
               </ListView.ItemTemplate>
            </ListView>

When changing the option in picker I need to get the current listview item, I tried with SelectedIndexChanged property. But always getting null value for selectedItem.

Following is my code:

private void AttendanceStatus(object sender, EventArgs args)
        {
            try
            {
                var item = sender as Picker;
                var selectedItem = item.SelectedItem as cbrainAttendanceHBList;
                if(selectedItem != null)
                {
                    Debug.WriteLine("name:>." + selectedItem.name);
                }
            }
            catch(Exception e)
            {
                Debug.WriteLine("Exception:>>"+e);
            }
        }

Can anyone suggest a way to capture the selected picker item in listview?

Thanks in advance :)


Solution

  • the Picker's ItemSource is a List<string>, so you cannot cast it as a cbrainAttendanceHBList. However, the Picker's BindingContext should be the cbrainAttendanceHBList for that row.

    var picker = sender as Picker;
    var selectedItem = picker.BindingContext as cbrainAttendanceHBList;