ListView doesn't update while typing on SearchView until I hide the keyboard

var007var007 Member ✭✭

Hello, I'm creating an Android app that connects to Azure Search Service. I added a SearchView and a ListView. The idea is that as I type on the SearchView, I get live results from Azure Search and I display them on the ListView as is common functionality.

The problem I have is that the ListView doesn't get updated while the Android keyboard is displayed. So initially the ListView is empty, I type a couple of letters in the SearchView, nothing appears on the ListView, but if I tap the Android back button (at this point a down button) to hide the Android keyboard then the ListView is populated and the search results appear.

If I type in 3, 4 or more letters I see the right results but only when I hide the keyboard.

This is basically my code:

private SearchView searchView;
private ListView listView;
private ArrayAdapter<string> adapter;

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    SetContentView(Resource.Layout.activity_main);

    searchView = FindViewById<SearchView>(Resource.Id.searchBox);
    listView = FindViewById<ListView>(Resource.Id.searchResult);

    adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1);
    listView.Adapter = adapter;

    searchView.QueryTextChange += SearchView_QueryTextChange;
}

private void SearchView_QueryTextChange(object sender, SearchView.QueryTextChangeEventArgs e)
{
    adapter.Clear();
    if (e.NewText.Length > 1)
    {
        searchTerm(e.NewText);
    }
}

private async void searchTerm(string newText)
{
    var client = new RestClient("mysearchengine.search.windows.net");
    var request = new RestRequest("indexes/myitems/docs/search", Method.POST);

    request.AddParameter("api-version", "2017-11-11", ParameterType.QueryString);
    request.AddHeader("api-key", "MYKEY");
    request.AddHeader("content-type", "application/json");
    request.RequestFormat = DataFormat.Json;
    request.AddBody(new { search = newText + "*", select = "id, title" });

    try {
        await Task.Run(() => {
        IRestResponse response = client.Execute(request);
        if (response.StatusDescription == "OK")
                    {
                        string values = response.Content;
                        List<MyPost> myPosts = JsonConvert.DeserializeObject<List<MyPost>>(values);
                        List<string> items = new List<string>();
                        for(int i=0;i<myPosts.Count;i++)
                        {
                            items.Add(myPosts[i].Title + " (" + myPosts[i].ID + ")");
                        }
                        adapter.AddAll(items);
                        adapter.NotifyDataSetChanged();
                    }
                });
    }
    catch(Exception e) { }
}

Thanks in advance!

Answers

  • BillyLiuBillyLiu Member, Xamarin Team Xamurai

    I think this issue may be caused by the network delay. I have tested your codes without using RestClient and it works fine.
    You could try to cache the data from Azure to local, then filter and fill in the data in your searchTerm() method.

  • var007var007 Member ✭✭

    Thank you, I will try again. I'm not sue what you mean by "cache the data from Azure to local, then filter and fill in the data". If you mean cache all the data and then filter it myself then that would defeat the purpose of using the Azure search engine.

Sign In or Register to comment.