locked
[UWP][C#]Displaying search results using textbox_changing on listview from JSON data RRS feed

  • Question

  • I have a textbox to do a search using textbox_textchanging and a listview. When the user typed the letter o, it will show the search result of the letter o on listview and when user typed the word omapukis, it will show the search result from The word omapukis.

    XAML:

    <TextBox 
        x:Name="searchBox" 
        Grid.Row="0" 
        Margin="10,0,10,10" 
        Text="" 
        Foreground="Black" 
        Background="#E6FDFDFD" 
        BorderBrush="#FF7A7A7A" 
        PlaceholderText="Search in Indonesia Products &amp; Bussiness"
        FontFamily="Segoe UI Black" 
        FontSize="16" 
        TextChanging="searchBox_TextChanging" />
    <ListView 
        x:Name="suggestionList" 
        Grid.RowSpan="2" 
        Grid.Column="1" 
        Margin="67,75,0,0" 
        HorizontalAlignment="Left" 
        VerticalAlignment="Top" 
        Width="1135" Height="400" 
        Background="White" 
        BorderBrush="Black" 
        BorderThickness="1"
        Visibility="Collapsed"
        AutomationProperties.AutomationId="ItemsListView"
        AutomationProperties.Name="Items"
        ItemsSource="{Binding Source={StaticResource itemsViewSource}}"
        IsItemClickEnabled="True" 
        ItemClick="suggestionList_ItemClick">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <StackPanel Grid.Column="0" >
                        <TextBlock 
                            x:Name="Nama" 
                            Text="{Binding Title}" 
                            Style="{StaticResource TitleTextBlockStyle}" 
                            TextWrapping="NoWrap" 
                            Visibility="Visible" 
                            Foreground="Black"
                            HorizontalAlignment="Left" 
                            Margin="10,0,10,10"
                            SelectionHighlightColor="#FFBFA342"
                            FontSize="16"/>
                        <TextBlock 
                            Text="{Binding ID}" 
                            Style="{StaticResource CaptionTextBlockStyle}" 
                            TextWrapping="NoWrap" 
                            Visibility="Collapsed"
                            Foreground="#FFAAAAAA"/>
                    </StackPanel>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
        <ListView.ItemContainerStyle>
            <Style TargetType="FrameworkElement">
                <Setter Property="Margin" Value="0,0,0,5"/>
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>

    Code:

    ObservableCollection<SearchClass> suggestionDatasourceDetail = new ObservableCollection<SearchClass>();
    ObservableCollection<SearchClass> historyDatasourceDetail = new ObservableCollection<SearchClass>();
    private async void searchBox_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args)
    {
        string keyword = searchBox.Text;
        suggestionList.ItemsSource = null;
        suggestionList.Items.Clear();
        suggestionDatasourceDetail.Clear();
        koneksiErrorStack.Visibility = Visibility.Collapsed;
        requestErrorStack.Visibility = Visibility.Collapsed;
    
        ConnectionProfile connections = NetworkInformation.GetInternetConnectionProfile();
        if (connections != null && connections.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess)
        {
            itemGridView.Visibility = Visibility.Visible;
            busyIndicator.IsActive = true;
    
            try
            {
                string urlPath = "http://.../suggest.json?q=" + keyword + "&module=listings&page=1&token=3f63-dc43-c8d5-eb45-8cbf-b72d-9d98-800f";
                var httpClient = new HttpClient(new HttpClientHandler());
    
                var values = new List<KeyValuePair<string, string>>{};
    
                HttpResponseMessage response = await httpClient.GetAsync(urlPath);
                response.EnsureSuccessStatusCode();
    
                if (!response.IsSuccessStatusCode)
                {
                    busyIndicator.IsActive = false;
                    RequestException();
                }
    
                string jsonText = await response.Content.ReadAsStringAsync();
                JsonObject jsonObject = JsonObject.Parse(jsonText);
                JsonArray jsonData = jsonObject["data"].GetArray();
    
                foreach (JsonValue groupValue1 in jsonData)
                {
                    JsonObject groupObject2 = groupValue1.GetObject();
    
                    double id = groupObject2["id"].GetNumber();
                    string title = groupObject2["title"].GetString();
                    string type = groupObject2.ContainsKey("type") && groupObject2["type"] != null ? groupObject2["type"].GetString() : string.Empty;
    
                    SearchClass file1 = new SearchClass();
                    file1.ID = Convert.ToInt32(id);
                    file1.Title = title;
    
                    if (type != string.Empty)
                    {
                        file1.Type = type;
                    }
                    if (file1.Type != "blog")
                    {
                        suggestionDatasourceDetail.Add(file1);
                    }
                }
                suggestionList.ItemsSource = searchDatasourceDetail;
    
                if (suggestionDatasourceDetail.Count == 0)
                {
                    busyIndicator.IsActive = false;
                    loading.Visibility = Visibility.Collapsed;
                    suggestionList.Visibility = Visibility.Collapsed;
                }
    
                else
                {
                    busyIndicator.IsActive = false;
                    loading.Visibility = Visibility.Collapsed;
                    suggestionList.Visibility = Visibility.Visible;
                }
            }
            catch (HttpRequestException ex)
            {
                busyIndicator.IsActive = false;
                loading.Visibility = Visibility.Collapsed;
                RequestException();
                suggestionList.Visibility = Visibility.Collapsed;
            }
        }
        else
        {
            busyIndicator.IsActive = false;
            loading.Visibility = Visibility.Collapsed;
            ConnectionException();
            suggestionList.Visibility = Visibility.Collapsed;
        }
    }

    I have a problem, when typing the letter o, it will show the search result of the letter o on listview, while when user type omapukis, then Will display the search results will show from oomomaomaomapukis. I want to display search results from the word omapukis only (search results with the last letter or word only).
    And after that I search for the word kalingga, then the search results will appear from omomaomaomapukiskakalikalingga. I want to display search results from the word kalinggaonly (search results with the last letter or word only).

    How to resolve it in order to display search results in listview of only the last letter or word entered by the user?



    Thursday, July 20, 2017 1:24 AM

All replies