SearchBar in Listview JSON

tecomunicotecomunico USMember ✭✭

Hi, Guys. Someone can explain me how search value in Listview ?
My XAML is:

My Code Behind is:

My Device is:

Thanks,

Best Answers

  • kizanlikkizanlik TR ✭✭
    edited August 2017 Accepted Answer

    hi @tecomunico

    Yes. AllDevice is null because you do not set it. It is just initialized at the beginning.

    I see two options. Modify your code and:

    1) Use AllDevice instead of allDevice while deserializing recevied data.
    2) Define allDevice in class not in LoadAll method and change this line:

    AllListView.ItemsSource = AllDevice.Where(x => x.UnidadNumero.Contains(searchText)); // <- Upper case A, AllDevice
    

    to

    AllListView.ItemsSource = allDevice.Where(x => x.UnidadNumero.Contains(searchText)); // <- Lower case a, allDevice
    
  • tecomunicotecomunico US ✭✭
    Accepted Answer

    If I use a Static List, work fine, but when i use:

    var allDevice = JsonConvert.DeserializeObject<List>(result);
    AllListView.ItemsSource = allDevice;

    Don't work.

  • tecomunicotecomunico US ✭✭
    Accepted Answer

    Thanks @kizanlik you are a GURU.
    Work Fine !!!!!!

Answers

  • kizanlikkizanlik TRMember ✭✭

    Hi @tecomunico

    Use Linq. Linq examples.

    For your code:

    using System.Linq; // <- must be added before namesapce definition
    ...    
    AllListView.ItemsSource = AllDevice.where(device => device.IDCustomer.Contains("SearchBox text goes here"));
    ...
    

    I didn't test the piece of code. Linq is the point here.

  • tecomunicotecomunico USMember ✭✭

    Thanks @kizanlik, but not work.

    My Code is:
    using System.Net.Http;
    using Xamarin.Forms;
    using System.MPG.Models;
    using System.Collections.Generic;
    using System.MPG.Cells;
    using Newtonsoft.Json;
    using System.Linq;

    namespace System.MPG
    {
    public partial class VerBusquedaPage : ContentPage
    {
    public List AllDevice = new List();

        public VerBusquedaPage()
        {
            InitializeComponent();
            AllListView.ItemTemplate = new DataTemplate(typeof(AllCells));
            LoadAll();
        }
    
        private void TextoCambiado(object sender, TextChangedEventArgs e)
        {
            string searchText = BarraSearchAll.Text;
            if (string.IsNullOrWhiteSpace(e.NewTextValue))
            {
                AllListView.ItemsSource = AllDevice;
            }else{
                AllListView.ItemsSource = AllDevice.Where(x => x.UnidadNumero.Contains(searchText));
            }
        }
    
        private async void LoadAll()
        {
            waitActivityIndicator.IsRunning = true;
            string result = string.Empty;
            try
            {
                HttpClient Cliente = new HttpClient();
                Cliente.BaseAddress = new Uri("http://www.mpg.work");
                string url = string.Format("/json/xamarin/json_buscarall.php");
                var response = await Cliente.GetAsync(url);
                result = response.Content.ReadAsStringAsync().Result;
            }
            catch (Exception ex)
            {
                await DisplayAlert("Error", string.Format("Your Smartphone Should Be Connected to Internet", ex.Message), "Ok");
                waitActivityIndicator.IsRunning = false;
                return;
            }
            waitActivityIndicator.IsRunning = false;
            var allDevice = JsonConvert.DeserializeObject<List<DeviceAll>>(result);
            AllListView.ItemsSource = allDevice;
        }
    }
    

    }

    Picture:

    Example:

    Listview Working: The Listview show 3 Json Fields, yellow field is a Unit Number (UnidadNumero)

    Not Show anything:

  • kizanlikkizanlik TRMember ✭✭
    edited August 2017 Accepted Answer

    hi @tecomunico

    Yes. AllDevice is null because you do not set it. It is just initialized at the beginning.

    I see two options. Modify your code and:

    1) Use AllDevice instead of allDevice while deserializing recevied data.
    2) Define allDevice in class not in LoadAll method and change this line:

    AllListView.ItemsSource = AllDevice.Where(x => x.UnidadNumero.Contains(searchText)); // <- Upper case A, AllDevice
    

    to

    AllListView.ItemsSource = allDevice.Where(x => x.UnidadNumero.Contains(searchText)); // <- Lower case a, allDevice
    
  • tecomunicotecomunico USMember ✭✭
    Accepted Answer

    If I use a Static List, work fine, but when i use:

    var allDevice = JsonConvert.DeserializeObject<List>(result);
    AllListView.ItemsSource = allDevice;

    Don't work.

  • tecomunicotecomunico USMember ✭✭
    Accepted Answer

    Thanks @kizanlik you are a GURU.
    Work Fine !!!!!!

  • tecomunicotecomunico USMember ✭✭

    Thanks @kizanlik

Sign In or Register to comment.