Home > Mobile >  How can I populate the Search bar field (Item Source) with data from the ListView when the ListView
How can I populate the Search bar field (Item Source) with data from the ListView when the ListView

Time:10-13

I created a Entry that shows addresses in the list view when text changed. It works well. Now I want to implement ItemTaped to populate the Entry to save that data (as I am saving/binding Entry results). I know how to do it for only one string (name), but I have 2 strings in my code (string search-changing text and string location-string formatedLocation in code name). Below is my code. So my question is how to write a code for ItemTapped(searchResults_ItemTapped-in code name) in order to ListView data populate my Entry field (locationText_Changed- in code name)? Thanks.

 private async void locationText_Changed(object sender, TextChangedEventArgs e)
    {
        try
        {           
        if (location == null)
        {
            await GetLocation();
        }
        string formattedLocation = string.Format("{0},{1}", location.Latitude, location.Longitude);
        List<Prediction> predictionList = await VenueLogic1.GetVenues1(e.NewTextValue, formattedLocation);      
            searchResults.ItemsSource = predictionList;
            
        } catch (NullReferenceException nulrex)
        {
        } catch (Exception except)
        {
        }
    }
    private void searchResults_ItemTapped(object sender, ItemTappedEventArgs e)
    {
    }

UI:

<Entry x:Name="locationEntry"
                       TextChanged="locationText_Changed"
                       Placeholder="Unesi Lokaciju"/>                  />
                    <ListView x:Name="searchResults" HorizontalOptions="FillAndExpand" ItemTapped="searchResults_ItemTapped" >
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <Label Text="{Binding description}"/>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>

CodePudding user response:

You have a reference to locationEntry by name, and you can use pattern matching to type check and cast ItemTappedEventArgs.Item to your search result type, then grab the description from that object.

private void searchResults_ItemTapped(object sender, ItemTappedEventArgs e)
{
    if (e.Item is MySearchResultClass result)
    {
        locationEntry.Text = result.description;
    }
}
  • Related