Callum McAlister
Callum McAlister

Reputation: 13

Xamarin Forms ListView SelectedItem to getting correct value

Thanks for your help. Let me assure you I have searched high and low for this issue.

I'm currently using Xamarin Forms to create a mobile application. I have a List View that is generated from a table in a database. This all works fine.

My List view populates fine with the text of each field, I want the user to select the TextCell in the list view and this is put into a variable / string.

At the moment every time i try to use ListExample.SelectedItem.ToString() I get the value of my Database Model class path. See images. I can see that the values are inside the text. I just cannot access them. Any help would be great.

<ListView x:Name="SearchResults" 
          ItemsSource="{Binding Post}" 
          ItemSelected="SearchResults_ItemSelected" 
          SelectedItem="{Binding OBJUNIQUEID, Mode=TwoWay}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextCell x:Name="selected" 
                      Height="15" 
                      TextColor="Black" 
                      Text="{Binding OBJUNIQUEID}"/>
        </DataTemplate>
    </ListView.ItemTemplate>

</ListView>

The c# code as is follows.

var results = conn.Query<Post>("SELECT * FROM OBJ_MASTER").ToList();

SearchResults.ItemsSource = results
CredoConsole.Text = "Item Selected: " + SearchResults.SelectedItem.ToString(); 

Image: Mobile App interface Bottom red text is wrong as you can see

If anyone has anything that can point me in the right direction.

Upvotes: 1

Views: 1787

Answers (1)

Jason
Jason

Reputation: 89204

protected void SearchResults_ItemSelected(object sender, SelectedItemChangedEventArgs e) {

  var item = (Post)e.SelectedItem;
  var id = item.OBJUNIQUEID;
}

Upvotes: 2

Related Questions