locked
AutoSuggestBox ItemsSource from SQLite database RRS feed

  • Question

  • User380485 posted

    I've got stuck with this, and am very new to Xamatin Forms. (I also posted this Friday but can find no trace of it???)

    I'm wanting to use the AutoSuggestBox from dotMorten in my Xamarin Forms app.

    I have the list of values in a SQLite table:

    public class Streets
    {
        [PrimaryKey]
        [MaxLength(255)]
        public String StreetName { get; set; }
    }
    

    I'm wanting to use this table as the ItemsSource for the AutoSuggestBox but, using the ToDo example have this code to get the data from the table:

    public Task<List<Streets>> LookupStreetsAsync(String query)
    {
        return lookups.Table<Streets>().Where(s => s.StreetName.StartsWith(query)).OrderBy(s => s.StreetName).ToListAsync();
    }
    

    However I cannot use this as the ItemsSource

    private void StreetAutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs e)
    {
        if (e.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
        {
            String query = sender.Text.Trim();
            // only auto suggest if 3 or more characters
            if (query.Length > 2)
            {
                sender.ItemsSource = App.Lookups.LookupStreetsAsync(query);
            }
        }
    }
    

    I get an error at sender.ItemsSource:

    Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Collections.Generic.List<XamarinFirst.Streets>>' to 'System.Collections.IList'. An explicit conversion exists (are you missing a cast?)
    

    I have tried casting it to an IList but nothing I find Googling works, just different errors.

    How can I use my SQLite table as the ItemsSource?

    Thanks, Mike

    Monday, April 8, 2019 10:12 AM

Answers

  • User74 posted

    @MikeBeale said: I've got stuck with this, and am very new to Xamatin Forms. (I also posted this Friday but can find no trace of it???)

    I'm wanting to use the AutoSuggestBox from dotMorten in my Xamarin Forms app.

    I have the list of values in a SQLite table:

    public class Streets { [PrimaryKey] [MaxLength(255)] public String StreetName { get; set; } }

    I'm wanting to use this table as the ItemsSource for the AutoSuggestBox but, using the ToDo example have this code to get the data from the table:

    public Task> LookupStreetsAsync(String query) { return lookups.Table().Where(s => s.StreetName.StartsWith(query)).OrderBy(s => s.StreetName).ToListAsync(); }

    However I cannot use this as the ItemsSource

    private void StreetAutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs e) { if (e.Reason == AutoSuggestionBoxTextChangeReason.UserInput) { String query = sender.Text.Trim(); // only auto suggest if 3 or more characters if (query.Length > 2) { sender.ItemsSource = App.Lookups.LookupStreetsAsync(query); } } }

    I get an error at sender.ItemsSource:

    Cannot implicitly convert type 'System.Threading.Tasks.Task>' to 'System.Collections.IList'. An explicit conversion exists (are you missing a cast?)

    I have tried casting it to an IList but nothing I find Googling works, just different errors.

    How can I use my SQLite table as the ItemsSource?

    Thanks, Mike

    You need to await the result of your method as that returns a task (the task returns the list once it has completed).

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, April 8, 2019 12:07 PM

All replies

  • User74 posted

    @MikeBeale said: I've got stuck with this, and am very new to Xamatin Forms. (I also posted this Friday but can find no trace of it???)

    I'm wanting to use the AutoSuggestBox from dotMorten in my Xamarin Forms app.

    I have the list of values in a SQLite table:

    public class Streets { [PrimaryKey] [MaxLength(255)] public String StreetName { get; set; } }

    I'm wanting to use this table as the ItemsSource for the AutoSuggestBox but, using the ToDo example have this code to get the data from the table:

    public Task> LookupStreetsAsync(String query) { return lookups.Table().Where(s => s.StreetName.StartsWith(query)).OrderBy(s => s.StreetName).ToListAsync(); }

    However I cannot use this as the ItemsSource

    private void StreetAutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs e) { if (e.Reason == AutoSuggestionBoxTextChangeReason.UserInput) { String query = sender.Text.Trim(); // only auto suggest if 3 or more characters if (query.Length > 2) { sender.ItemsSource = App.Lookups.LookupStreetsAsync(query); } } }

    I get an error at sender.ItemsSource:

    Cannot implicitly convert type 'System.Threading.Tasks.Task>' to 'System.Collections.IList'. An explicit conversion exists (are you missing a cast?)

    I have tried casting it to an IList but nothing I find Googling works, just different errors.

    How can I use my SQLite table as the ItemsSource?

    Thanks, Mike

    You need to await the result of your method as that returns a task (the task returns the list once it has completed).

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, April 8, 2019 12:07 PM
  • User380485 posted

    Thanks John,

    such a simple fix and it's working great now. Seems the error I was getting was misleading!

    Anyway, thanks for your help.

    Monday, April 8, 2019 1:15 PM
  • User74 posted

    @MikeBeale said: Thanks John,

    such a simple fix and it's working great now. Seems the error I was getting was misleading!

    Anyway, thanks for your help.

    Hi Mike, Actually the error is accurate, you cannot cast a Task to an IList. ItemsSource is expecting an IList, not a Task, which is what the error message is telling you. I looked at the error you had before looking at your code.

    Cheers John

    Monday, April 8, 2019 1:25 PM
  • User380485 posted

    Ah okay, I see it now, I was paying more attention to the "System.Collections.Generic.List" part rather than the "System.Threading.Tasks.Task" part of the error.

    Many thanks

    Monday, April 8, 2019 1:27 PM
  • User393224 posted

    the autosuggestbox is filling with data, but I only got App1.Streets that appears in list, not the name of the Streets. It must be something small, but I can't figure it out. any ideas.

    (my Streets-table contains 5 columns)

    Saturday, March 7, 2020 2:28 PM