searchBar not change the Placeholder after clicked on ListView event listview_ItemSelected

50 Views Asked by At

I set a searchBar and ListView on my MainPage like that:

              <StackLayout>  
                    <SearchBar
                       BackgroundColor="#d3d3d3"
                       x:Name="searchBar"
                       Placeholder="Enter city"
                       PlaceholderColor="#919191"
                       TextChanged="SearchBar_TextChanged"
                       SearchButtonPressed="Btn_Search">
                    </SearchBar>

                   <Frame CornerRadius="15"
                           OutlineColor="#919191" 
                           Padding="2"
                           Margin="3">
                         
                       <Label  BackgroundColor="#dedede"
                               TextColor="#919191"
                               Font="Bold"
                               FontSize="20"
                               Text="Списък с населени места"
                               YAlign="Center"
                               XAlign="Center"
                               LineBreakMode="TailTruncation" />
                </Frame>
          
                      <ListView
                        BackgroundColor="#d3d3d3"
                        ItemTapped="ListView_ItemTapped"
                        ItemSelected="listView_ItemSelected"
                        HeightRequest="88"
                        x:Name="listView" >  
                        <ListView.ItemTemplate>  
                            <DataTemplate>  
                                <TextCell Text="{Binding Place}"/>
                            </DataTemplate>  
                        </ListView.ItemTemplate>  
                    </ListView>      
               </StackLayout>

After that I created class Contacts with String "Place" -> get; set;

public class Contacts
        {
            public string Place { get; set; }
        }

And open database connection for execute one query and fill my list like that:

 IEnumerable<Contacts> GetContacts(string searchText = null)
    {
        
        server = "ip-to-db";
        database = "db-name";
        uid = "user";
        password = "pass";
        string connectionString;
        connectionString = "SERVER=" + server + ";" + "DATABASE=" +
        database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
        connection = new MySqlConnection(connectionString);
        connection.Open();
        var cmd = new MySqlCommand();
        cmd.Connection = connection;

        //save from database data
        var contacts = new List<Contacts>();

        //cut first four strings from selected query
        MySqlCommand command = new MySqlCommand($"SELECT SUBSTRING(place,  4, 35), code AS ExtractString FROM places ORDER BY place", connection);

        using (MySqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                var place = new Contacts
                {
                    Place = reader[0].ToString() + " - " + reader[1].ToString()
                };

                contacts.Add(place);       

            }
            connection.Close();
        }

        if (string.IsNullOrEmpty(searchText))
            return contacts;
        return contacts.Where(p => p.Place.StartsWith(searchText));
    }


    private void ListView_Refreshing(object sender, EventArgs e)
    {
        listView.ItemsSource = GetContacts();
        
        listView.EndRefresh();
    }

    void SearchBar_TextChanged(System.Object sender, Xamarin.Forms.TextChangedEventArgs e)
    {
        listView.ItemsSource = data.Where(p => p.Place.StartsWith(e.NewTextValue));
    }

Now I want when click on the some ListView element to change my placeholder on the searchBar, but when I debug the last one of code under this text I see the string from listView.SelectedItem.ToString();, but when I want to display that I not see the string on the display with this code:

 void listView_ItemSelected(System.Object sender, Xamarin.Forms.SelectedItemChangedEventArgs e)
    {

        LabelClick.Text = listView.SelectedItem.ToString();

        searchBar.Placeholder = LabelClick.Text;
}

So this is screen shot what exactly display on the searchBar when I click on some listView element. searchBar after clicked on ListView

1

There are 1 best solutions below

2
On BEST ANSWER

the default behavior of ToString is to return the name of the class. That is exactly what is happening. If you want to display the Place property you need to specify that

searchBar.Placeholder = ((Contacts)listView.SelectedItem).Place;