Issue
Use: Xamarin on Visual Studio for Mac
I have a sample SearchBar with filled ListView from database with places. When I start the app I see the places in the ListView, but when I click on the SearchBar and want to search some item from database the SearchBar is stuck.. Example: I try to enter "Plovdiv".. but I can't ... just Enter P... after 25 second the search enter "l"... after 40 second i see the other "o".. and SearchBar work super slowly for hint...
My code from MainPage.cs:
public partial class MainPage : ContentPage
{
public string GlobalLat;
public string GlobalLong;
private MySqlConnection connection;
private string server;
private string database;
private string uid;
private string password;
public MainPage()
{
InitializeComponent();
listView.ItemsSource = GetContacts();
this.BindingContext = this;
Task task = GetUserLocationAsync();
}
public class Contacts
{
public string Place { get; set; }
}
IEnumerable<Contacts> GetContacts(string searchText = null)
{
server = "192.168.0.1,3306";
database = "dbName";
uid = "username";
password = "";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
connection.Open();
var cmd = new MySqlCommand();
cmd.Connection = connection;
var contacts = new List<Contacts>();
MySqlCommand command = new MySqlCommand($"SELECT place FROM places ORDER BY place", connection);
using (MySqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
var place = new Contacts
{
Place = reader[0].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 = GetContacts(e.NewTextValue);
}
void Btn_Search(System.Object sender, System.EventArgs e)
{
}
}
This is the code from MainPage.xaml file:
<StackLayout>
<SearchBar
Placeholder="Enter city"
TextChanged="SearchBar_TextChanged"
SearchButtonPressed="Btn_Search"></SearchBar>
<ListView
HeightRequest="250"
x:Name="listView" >
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Place}">
</TextCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
So the Search work hyper slowly and He stuck every time when I hit on the her and want to search..
Is there a way to fix that ?
Solution
first, keep a local list of Contacts, you do not need to refresh from the remote DB every time
List<Contact> data;
public MainPage()
{
InitializeComponent();
listView.ItemsSource = data = GetContacts();
this.BindingContext = this;
Task task = GetUserLocationAsync();
}
then, when you search, use the data you already have loaded, do NOT continually query the remote database
void SearchBar_TextChanged(System.Object sender, Xamarin.Forms.TextChangedEventArgs e)
{
listView.ItemsSource = data.Where(p => p.Place.StartsWith(e.NewTextValue));
}
Answered By - Jason
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.