Retrieving multiple values from a selected row in List View

I am binding multiple values to the Listview in Xamarin and when I selected a single row I want to retrieve the multiple values from that. Here is the blow code I have written for retrieving the values

try
{
if (((ListView)sender).SelectedItem == null)
{
return;
}
else
{

                var patients = (Patient)((ListView)sender).SelectedItem;
                entryPatientName.Text = patients.PatientName;
                patientid =Convert.ToInt32(patients.PaitentId);

            }

By using the above code I am getting the selected patient name and I am unable to get the id of the selected patient can any help on this.

Best Answers

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    WHat is ID? If ID is a property in your Model, you have it in SelectedItem. If ID is the position of SelectedItem inside the list, you can have it with something like MyList.IndexOf(SelectedItem)
  • viharipviharip Member ✭✭

    ID is person unique code if I take the indexOf(SelectedItem) it will get the order of elements which they are arranged but the index and the id are different

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    I think ID should be a property like PatientName
  • viharipviharip Member ✭✭

    I am getting the PatientName but unable to get the Selected ID

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Post you "Model"

  • viharipviharip Member ✭✭

    I am binding directly from JSON format
    public class Patient
    {

            public string PatientName { get; set; }
            public string PaitentId { get; set; }
    
        }
    
  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    PaitentId is your ID...

  • ColeXColeX Member, Xamarin Team Xamurai

    Could you debug and post the value of patients.PaitentId ? If you want to convert it from string to int , ensure that it should consist of number only .

  • viharipviharip Member ✭✭

    @AlessandroCaliaro said:
    PaitentId is your ID...

    Yes

  • viharipviharip Member ✭✭

    @ColeX said:
    Could you debug and post the value of patients.PaitentId ? If you want to convert it from string to int , ensure that it should consist of number only .

    yes it's getting zero

  • ColeXColeX Member, Xamarin Team Xamurai

    What's the value of patients.PaitentId ? Could you post here ?

  • viharipviharip Member ✭✭

    @AlessandroCaliaro said:
    @viharip , you have the value… maybe it's wrong, but it's stored in PaitentId property… Could be a wrong name? Could be PatientId…???

    When i am binding to > @ColeX said:

    What's the value of patients.PaitentId ? Could you post here ?

    it should get 3 digit number

  • ColeXColeX Member, Xamarin Team Xamurai
    edited August 26

    My first guess is the value over range Int32 , so try to use Int64.Parse(a) or Convert.ToInt64(a) instead.

    However , you told me it should be 3 digit number ... Could you attach a basic sample project? I could test on my side for further troubleshooting.

  • viharipviharip Member ✭✭

    @ColeX said:
    My first guess is the value over range Int32 , so try to use Int64.Parse(a) or Convert.ToInt64(a) instead.

    However , you told me it should be 3 digit number ... Could you attach a basic sample project? I could test on my side for further troubleshooting.

    value is not getting on selected value i m getting the patientname but unable to get the patient id

  • viharipviharip Member ✭✭

    @AlessandroCaliaro said:
    @viharip , you have the value… maybe it's wrong, but it's stored in PaitentId property… Could be a wrong name? Could be PatientId…???

    ya check PatientID value is not displaying in the listview and I gave all the name are same

  • ColeXColeX Member, Xamarin Team Xamurai

    Could you share us a basic sample project for reproducing?

  • viharipviharip Member ✭✭
    edited August 26

    @ColeX said:
    Could you share us a basic sample project for reproducing?

    <ListView x:Name="listviewPatients"   ItemSelected="ListviewPatients_ItemSelected" Grid.Row="1" HorizontalOptions="FillAndExpand" HasUnevenRows="True" >
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <ViewCell>
                                        <Grid HorizontalOptions="FillAndExpand" Padding="2">
                                            <Grid.RowDefinitions>
                                                <RowDefinition Height="Auto"/>
                                                <RowDefinition Height="Auto"/>
                                                <RowDefinition Height="Auto"/>
    
                                            </Grid.RowDefinitions>
    
    
                                            <Label Text="{Binding Fee}"   Grid.Row="0" TextColor="Black"  FontAttributes="Bold"/>
                                            <Label Text="{Binding PatientId}"   Grid.Row="1"  BackgroundColor="Red" TextColor="Gold"  FontAttributes="Bold"/>
                                            <Label Text="{Binding PatientName}"  BackgroundColor="Black"  Grid.Row="2" TextColor="Orange"  FontAttributes="Bold"/>
    
    
    
                                        </Grid>
                                    </ViewCell>
    
    
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
    

    from json i m binding the data

    public string PatientName { get; set; }
    public string PaitentId { get; set; }

    }
    
Sign In or Register to comment.