Question

Hello Friends I am looking for a solution to an XML parsing below is my database sample :-

        <inneritem id="1" title="Cells" thumbnail="">
            <Seconditem id="3" title="Intro" thumbnail="" />
            <Seconditem id="4" title="Erythroc" thumbnail="" />
            <Seconditem id="5" title="Band Nerophil" thumbnail="" />
            <Seconditem id="6" title="Segmented Ntrophil" thumbnail="" />
            <Seconditem id="7" title="Eosinil" thumbnail="" />
        </inneritem>

        <inneritem id="2" title="Cells" thumbnail="">
            <Seconditem id="8" title="2Intro" thumbnail="" />
            <Seconditem id="9" title="2Erythroc" thumbnail="" />
            <Seconditem id="10" title="2Band Nerophil" thumbnail="" />
            <Seconditem id="11" title="2Segmented Ntrophil" thumbnail="" />
            <Seconditem id="12" title="2Eosinil" thumbnail="" />
        </inneritem>

Now here I had set Item with id 1 and 2 in the listview and on click on any item for example if I click on Item with id 1 it will give me id 1 and on its click I want to load data with tag Seconditem related to id 1 into new listview.So how can I accomplish that. Thanks in advance.

In Short I want to check condition of id's and then load data so how can I check these conditions. My Code for the same is :-

            try {

                InputStream is = getActivity().getAssets().open("public_material.xml");

                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();


                Document doc = dBuilder.parse(is);
                doc.getDocumentElement().normalize();

                doc.getElementsByTagNameNS("id", ItemsID);
                NodeList nList = doc.getElementsByTagName("Seconditem");
                Log.d("List count :- ", "" + nList.getLength());
                for (int temp = 0; temp < nList.getLength(); temp++) {
                    Node GetEementNode = nList.item(temp);
                    if (GetEementNode.getNodeType() == Node.ELEMENT_NODE) {


                        Element eElement = (Element) GetEementNode;


                        String strID = eElement.getAttribute("id");

                        String Title = eElement.getAttribute("title");
                        //String Thumbnail = eElement.getAttribute("thumbnail");


//                      GetterSetter row = new GetterSetter(strID,Title, Thumbnail);
                        GetterSetter row = new GetterSetter(strID,Title);
                        arrList.add(row);
                    }
                }

            } catch (Exception e) {

            }

            BaseAdapterData data = new BaseAdapterData(getActivity(), arrList);

            list2NewView.setAdapter(data);
Was it helpful?

Solution

This is what I was looking for where public_material is my xml in which data is saved:-

try{InputStream is = getActivity().getAssets().open("public_material.xml");

                    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();


                    doc = dBuilder.parse(is);
                    doc.getDocumentElement().normalize();

                    nList = doc.getElementsByTagName("inneritem");
                    for (int temp = 0; temp < nList.getLength(); temp++) {
                        Node GetEementNode = nList.item(temp);
                        if (GetEementNode.getNodeType() == Node.ELEMENT_NODE) {

                            eElement = (Element) GetEementNode;
                            String strID = eElement.getAttribute("id");

                            if(strID.equals(ItemsID)){
                                NodeList conditionList = eElement.getElementsByTagName("Seconditem");

                                for(int secondTemp = 0; secondTemp<conditionList.getLength();secondTemp++){
                                    Node getSecondNode = conditionList.item(secondTemp);
                                    if(getSecondNode.getNodeType() == Node.ELEMENT_NODE){
                                        Element SecondElement  = (Element) getSecondNode;
                                        String secondItemsDataID = SecondElement.getAttribute("id");
                                        String secondItemsData = SecondElement.getAttribute("title");
                                        GetterSetter row = new GetterSetter(secondItemsDataID,secondItemsData);
                                        arrList.add(row);

                                    }
                                }
                            }
    //                      String Title = eElement.getAttribute("title");
                            //String Thumbnail = eElement.getAttribute("thumbnail");


    //                      GetterSetter row = new GetterSetter(strID,Title, Thumbnail);
    //                      GetterSetter row = new GetterSetter(strID,Title);
    //                      arrList.add(row);
                        }
                    }

                } catch (Exception e) {

                }

OTHER TIPS

on a click of listview get the id and compare it with the arrList.getStrId() in for loop and if matches get the data and do whatever you want....

Try below code:

list2NewView.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
            {
                  if(list2NewView.get(arg2).toString().equals(arrList.get(arg2).toString())
                  {
                      // do your code here
                  }
            }
        });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top