Filter Longlistselector [CATEGORIES]

Asked

Viewed 79 times

-1

Hello, I have a webservice that returns me a list of "categories" with their sub-categories .

I need help to "filter this", for example:

Category 1 has sub-category 1 and 2;

Category 2 has sub-category 3;

Category 3 has sub-category 4, 5 and 6;

I want that when selecting the category 1, come in the next screen the sub-categories 1 and 2.

My web-service, already returns to me all categories and sub-categories of each, I want to know how to handle them to operate this way..

The parse made to pick up the category codes, would be these and are added to an Observablecollection:

string strDataTable = e.Result;

        try
        {
            if (strDataTable.Length < 20)
            {
                MessageBox.Show("Não há dados disponíveis no momento, tente novamente mais tarde.\nCaso o erro persista, contactar o suporte técnico.");
                return;
            }
            XElement doc = XElement.Parse(strDataTable);

            foreach (XElement row in doc.Elements("GWDataTable"))
            {
                Produtos produto = new Produtos();

                produto.descCateg = row.Element("DESCRICAOCATEGORIA").Value.ToString();
                produto.descFamil = row.Element("DESCRICAOFAMILIA").Value.ToString();
                produto.descGrupo = row.Element("DESCRICAOGRUPO").Value.ToString();
                produto.descReceita = row.Element("DESCRICAOCRECEITA").Value.ToString();
                produto.descricao = row.Element("OBS").Value.ToString();
                produto.produto = row.Element("DESCRICAOPROD").Value.ToString();
                produto.unidade = row.Element("UNIDADE").Value.ToString();
                produto.precoN = row.Element("PRECONORMAL").Value.ToString();
                produto.precoP = row.Element("PRECOPROMOCIONAL").Value.ToString();

                produto.codCategoria = row.Element("CODCATEGORIA").Value.ToString();
                produto.codFamilia = row.Element("CODFAMILIA").Value.ToString();
                produto.codGrupo = row.Element("CODGRUPO").Value.ToString();
                produto.codReceita = row.Element("CODCRECEITA").Value.ToString();




                Resultados.ResProdutos.Add(produto);
            }
            try
            {
                SelectorProdutos.ItemsSource = Resultados.ResProdutos;
                SetProgressIndicator(false);
            }
            catch (Exception)
            {
                MessageBox.Show("ERRO");
            }

        }


Resultados.ResProdutos.Add(produto);}

Products:

public class Produtos
{
    public string descReceita { get; set; }
    public string descGrupo { get; set; }
    public string descCateg { get; set; }
    public string descFamil { get; set; }



    public string produto { get; set; }
    public string codReceita { get; set; }
    public string codGrupo { get; set; }
    public string codCategoria { get; set; }
    public string codFamilia { get; set; }
    public string precoN { get; set; }
    public string precoP { get; set; }
    public string unidade { get; set; }
    public string descricao { get; set; }

}
}

I prefer to use a Selectionchanged to start from the user by clicking on the Longlistselector item, move to the next screen, with the next category:

private void SelectorProdutos_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        if (SelectorProdutos.SelectedItem != null)
        {

            NavigationService.Navigate(new Uri("/Telas/frm5Grupo.xaml", UriKind.Relative));
        }
        else
        {
            MessageBox.Show("Erro");
        }
    }

At first I send to the screen the CODRECEITA for my Longlistselector, for the user to select the item you want, but after being selected, how can I proceed the filtering?

Web service response XML code:

<string xmlns="http://tempuri.org/">
<NewDataSet>
 <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
 <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="GWDataTable" msdata:UseCurrentLocale="true">
 <xs:complexType>
 <xs:choice minOccurs="0" maxOccurs="unbounded">
 <xs:element name="GWDataTable">
 <xs:complexType> 
 <xs:sequence> 
 <xs:element name="CODPROD" type="xs:int" minOccurs="0" />
 <xs:element name="DESCRICAOPROD" type="xs:string" minOccurs="0" />
 <xs:element name="CODCRECEITA" type="xs:int" minOccurs="0" />
 <xs:element name="DESCRICAOCRECEITA" type="xs:string" minOccurs="0" />
 <xs:element name="CODGRUPO" type="xs:int" minOccurs="0" />
 <xs:element name="DESCRICAOGRUPO" type="xs:string" minOccurs="0" />
 <xs:element name="CODCATEGORIA" type="xs:int" minOccurs="0" />
 <xs:element name="DESCRICAOCATEGORIA" type="xs:string" minOccurs="0" />
 <xs:element name="CODFAMILIA" type="xs:int" minOccurs="0" />
 <xs:element name="DESCRICAOFAMILIA" type="xs:string" minOccurs="0" />
 <xs:element name="PRECONORMAL" type="xs:double" minOccurs="0" />
 <xs:element name="PRECOPROMOCIONAL" type="xs:double" minOccurs="0" />
 <xs:element name="UNIDADE" type="xs:string" minOccurs="0" />
 <xs:element name="OBS" type="xs:string" minOccurs="0" />
 </xs:sequence>
 </xs:complexType>
 </xs:element>
 </xs:choice>
 </xs:complexType>
 </xs:element>
 </xs:schema>
 <GWDataTable>
 <CODPROD>1</CODPROD>
 <DESCRICAOPROD>CERVEJA ANTARCTICA</DESCRICAOPROD>
 <CODCRECEITA>12</CODCRECEITA>
 <DESCRICAOCRECEITA>BEBIDAS</DESCRICAOCRECEITA>
 <CODGRUPO>15</CODGRUPO>
 <DESCRICAOGRUPO>BEBIBAS ALCOOLICA</DESCRICAOGRUPO>
 <CODCATEGORIA>200</CODCATEGORIA>
 <DESCRICAOCATEGORIA>EM LATA</DESCRICAOCATEGORIA>
 <CODFAMILIA>105</CODFAMILIA>
 <DESCRICAOFAMILIA>CERVEJA</DESCRICAOFAMILIA>
 <PRECONORMAL>2.6</PRECONORMAL>
 <PRECOPROMOCIONAL>2.3</PRECOPROMOCIONAL>
 <UNIDADE>LATA</UNIDADE>
 <OBS>Quantidade máxima por cliente (5).</OBS>
 </GWDataTable>
 <GWDataTable>
 <CODPROD>14</CODPROD>
 <DESCRICAOPROD>AMACIANTE OMO</DESCRICAOPROD>
 <CODCRECEITA>19</CODCRECEITA> 
 <DESCRICAOCRECEITA>LIMPEZA</DESCRICAOCRECEITA> 
 <CODGRUPO>121</CODGRUPO>
 <DESCRICAOGRUPO>LAVA ROUPAS</DESCRICAOGRUPO>
 <CODCATEGORIA>76</CODCATEGORIA>
 <DESCRICAOCATEGORIA>EM PÓ</DESCRICAOCATEGORIA>
 <CODFAMILIA>15</CODFAMILIA> 
 <DESCRICAOFAMILIA>AMACIANTES</DESCRICAOFAMILIA>
 <PRECONORMAL>7.4</PRECONORMAL>
 <PRECOPROMOCIONAL>6.99</PRECOPROMOCIONAL> 
 <UNIDADE>CX</UNIDADE>
 <OBS>Validade da oferta 15/05.</OBS>
 </GWDataTable>
 </NewDataSet>
 </string>
  • Do you have a technical question or a process question? We don’t know your screen here and usually the posts here are about technical doubts. We can’t help just reading cat 1,2,3... and what you want to do. If there is a technical problem of using technology, put more information in the question and be more specific. You speak of Webservice and Canvas both were not shown.

  • @Malkavian You can see him now.

1 answer

1


As I understand it is a matter of process, you will use this category selection and pull back the data from Webservice only the subcategories of the selected category. In the navigate pass the category as parameter to query the new page you had opened and show the subcategories. If you don’t know how, put in more information about Webservice calls that we help.

Example:

NavigationService.Navigate(new Uri("/Telas/frm5Grupo.xaml?categoria=1", UriKind.Relative));

Note that 1 is example, you will enter the category that the user has selected, use the string. Format or concatene string.

Edit: Answering your question about always reusing the same collection during the life stages of the project, take a look at static and beware of concurrent access and app memory usage.

Edit2: Let’s say produtos be your collection of items, follow a snipet using your class Produtos.

List<Produtos> produtos = new List<Produtos> {
        new Produtos { codCategoria = "1" },
        new Produtos { codCategoria = "2" },
        new Produtos { codCategoria = "3" },
        new Produtos { codCategoria = "4" },
        new Produtos { codCategoria = "5" },
        new Produtos { codCategoria = "6" },
        new Produtos { codCategoria = "7" },
    };

    var resultado = produtos.FindAll(p => p.codCategoria == "7");

p is each product within the collection, then you write the logic for which every value with TRUE expression result the product will be selected. The result is another collection with only the chosen ones. Remember that the difference between Findall and Where is the return, one returns a copy of the elements in another collection the other just a enumerable.

In your case maybe Where consumes less memory, because the collection is static you just need to filter the same collection to show based on the user filter.

  • is what I wish to do, the problem is that all the information comes from the same webservice, I will be putting an example xml of the same answer

  • I pass all the information from the call to a list, would like to know how to reuse the list for the next steps, do not know if it is possible

  • I’ve added a resource to your response that can help you.

  • I heard about using Findall to find all the items of such a category, would you explain it to me? about Static, I always use in my classes that use 2 or more screens..

  • I put an example of generic Findall for you.

  • was really him @Malkaviano, thank you very much guy!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.