Home > Enterprise >  How can I display in UI a list of predictions (from my backend code when 'Text_Changed' in
How can I display in UI a list of predictions (from my backend code when 'Text_Changed' in

Time:10-11

my backend code with places autosuggestion works well (I can see it reads the locations from the list in my backend) Now I want to show that list to users when he/she starts to type something in entry by using Text_Changed option. Below is my Text_Changed method ("locationText_Changed"). Now how to show the Prediction list to a user in UI when he/she starts to type some text?

Backend code:

private async void locationText_Changed(object sender, TextChangedEventArgs e)
    {
        if (location == null)
        {
            await GetLocation();
        }
        string formattedLocation = string.Format("{0},{1}", location.Latitude, location.Longitude);
        List<Prediction> predictionList = await VenueLogic1.GetVenues1(e.NewTextValue, formattedLocation);

And my UI:

<StackLayout BackgroundColor="BlanchedAlmond" >
                <Entry x:Name="locationEntry"
                   TextChanged="locationText_Changed"
                   Placeholder="Enter Address"/>
            </StackLayout>

CodePudding user response:

First: To you show this list you need to use a ListView (it's better in questions of performance);

Second: The entry could be a SearchBar.

So, what you can do:

  • Create a searchBar, the user will input some data on it.
  • While the user is typing, your code will filtering the list and only will appear what matches with the input (as you mentioned).

In this link it shows step by step of how to achieve it. So maybe you will change something in your code, but the result is professional.

PS: Try to do it only following this link. If has any doubt, please, update your question with what you did and we will help you. Have fun.

CodePudding user response:

You can use databinding and collectionview/listview, set the ItemsSource as your predictionList, then change the itemsSourece in locationText_Changed method.like:

    private async void locationText_Changed(object sender, TextChangedEventArgs e)
        {
            if (location == null)
            {
                await GetLocation();
            }
            string formattedLocation = string.Format("{0},{1}", location.Latitude, location.Longitude);
            List<Prediction> predictionList = await VenueLogic1.GetVenues1(e.NewTextValue, formattedLocation);
var newlist=from n in predictionList where n.contains(text)
select n;
mycollectionlist.ItemsSource=newlist;}
  • Related