0

Want to fetch only the unique values from the XML which i am getting in my spinner.... i am able to get the values in my spinner but all dublicate values are also showing in spinner dropdown.

For example my xml is

<a:AAAA>
<a:Q>123</a:Q>
<a:W>yessssssss</a:W>   <--- THIS VALUE IS SAME 
<a:E>275</a:E>                   ^
<a:R>wwwwqwq</a:R>               |
</a:AAAA>
             So want only single value in andrid spinner how to achive this?                                                               
<a:AAAA>                         |
<a:Q>456</a:Q>                   v
<a:W>yessssssss</a:W>   <--- AND THIS VALUE IS SAME 
<a:E>648</a:E>
<a:R>qwqwqsd</a:R>
</a:AAAA> 

<a:AAAA>
<a:Q>789</a:Q>
<a:W>Hiiii</a:W>
<a:E>269</a:E>
<a:R>ds</a:R>
</a:AAAA>

<a:AAAA>
<a:Q>867</a:Q>
<a:W>qwqwqw</a:W>
<a:E>1648</a:E>
<a:R>wqw</a:R>
</a:AAAA>

below code is showing the value in spinner but with duplicate data

    public void onNothingSelected(AdapterView<?> arg0) {
    }

    protected void parse() {
        // TODO Auto-generated method stub


        try {

            URL url = new URL(
                    "web");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(url.openStream()));
            doc.getDocumentElement().normalize();

            NodeList nodeList = doc.getElementsByTagName("a:AAAA");
            for (int i = 0; i < nodeList.getLength(); i++) {

                Node node = nodeList.item(i);       

                Element fstElmnt = (Element) node;
                NodeList nameList = fstElmnt.getElementsByTagName("a:Q");
                Element nameElement = (Element) nameList.item(0);
                nameList = nameElement.getChildNodes();         


                NodeList websiteList = fstElmnt.getElementsByTagName("a:W");
                Element websiteElement = (Element) websiteList.item(0);
                websiteList = websiteElement.getChildNodes();

                NodeList websiteList1 = fstElmnt.getElementsByTagName("a:E");
                Element websiteElement1 = (Element) websiteList1.item(0);
                websiteList1 = websiteElement1.getChildNodes();

                NodeList websiteList2 = fstElmnt.getElementsByTagName("a:R");
                Element websiteElement2 = (Element) websiteList2.item(0);
                websiteList2 = websiteElement2.getChildNodes();


                title.add(((Node) nameList.item(0)).getNodeValue()+":"+((Node) websiteList.item(0)).getNodeValue() +"\n"+((Node) websiteList1.item(0)).getNodeValue()+"-"+((Node) websiteList2.item(0)).getNodeValue());

            }

EDITED-

    public void onNothingSelected(AdapterView<?> arg0) {
    }

    protected void parse() {
        // TODO Auto-generated method stub


        try {

            URL url = new URL(
                    "web");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(url.openStream()));
            doc.getDocumentElement().normalize();

            NodeList nodeList = doc.getElementsByTagName("a:AAAA");
            for (int i = 0; i < nodeList.getLength(); i++) {

                Node node = nodeList.item(i);       

                Element fstElmnt = (Element) node;
                NodeList nameList = fstElmnt.getElementsByTagName("a:Q");
                Element nameElement = (Element) nameList.item(0);
                nameList = nameElement.getChildNodes();         


                NodeList websiteList = fstElmnt.getElementsByTagName("a:W");
                Element websiteElement = (Element) websiteList.item(0);
                websiteList = websiteElement.getChildNodes();

                NodeList websiteList1 = fstElmnt.getElementsByTagName("a:E");
                Element websiteElement1 = (Element) websiteList1.item(0);
                websiteList1 = websiteElement1.getChildNodes();

                NodeList websiteList2 = fstElmnt.getElementsByTagName("a:R");
                Element websiteElement2 = (Element) websiteList2.item(0);
                websiteList2 = websiteElement2.getChildNodes();


                title.add(((Node) nameList.item(0)).getNodeValue()+":"+((Node) websiteList.item(0)).getNodeValue() +"\n"+((Node) websiteList1.item(0)).getNodeValue()+"-"+((Node) websiteList2.item(0)).getNodeValue());
                Set<String> uniqueTitles = new HashSet<String>(title);
                title3 = new ArrayList<String>(uniqueTitles);
            }
user1919
  • 309
  • 5
  • 15

1 Answers1

0

You can convert your data collection to set. This will remove duplicated items. After this conversion you can convert it back to list and use in application.

So if you are using title list to store spinner items, you can do something like this

Set<String> uniqueTitles = new HashSet<String>(title);
title = new ArrayList<String>(uniqueTitles)'
Kamil
  • 2,712
  • 32
  • 39
  • Sure, here you have documentation of ``java.util.Set`` class http://docs.oracle.com/javase/6/docs/api/java/util/Set.html and ``java.util.List`` documentation http://docs.oracle.com/javase/6/docs/api/java/util/List.html. Another topic about maintaining unique items in lists from StackOverflow that can be helpful http://stackoverflow.com/questions/13259535/how-to-maintain-a-unique-list-in-java. – Kamil Apr 09 '16 at 09:32
  • It is still giving the same duplicated values in the spinner....i am getting the dynamic value from the XML url every 1 minutes – user1919 Apr 09 '16 at 09:43
  • need your help please ? – user1919 Apr 09 '16 at 11:29
  • Try to put the two last lines of code ``Set uniqueTitles = new HashSet(title); title3 = new ArrayList(uniqueTitles);`` outside of the for-loop statement. – Kamil Apr 09 '16 at 11:43
  • 1
    how could i add the search functionality in spinner as if there is value inside spinner say "america" then after typing "ame" it should populate all the world starts with ame – user1919 Apr 11 '16 at 09:56
  • Check those topics on Stack Overflow http://stackoverflow.com/questions/28945310/android-searchable-dropdown-spinner, http://stackoverflow.com/questions/5801712/creating-a-text-filter-like-quick-search-for-a-spinner-in-android, http://stackoverflow.com/questions/26097932/how-to-set-search-interface-in-a-custom-spinner-in-android, http://stackoverflow.com/questions/10379829/how-to-add-search-option-to-android-spinner. If you need more help, please create separated topic, or try to search on Stack Overflow. – Kamil Apr 12 '16 at 05:37
  • sir i am need of you please see to it it will be a great thanks of yours ....http://stackoverflow.com/questions/36589263/how-to-display-two-spinners-with-linked-data – user1919 Apr 14 '16 at 08:43