Want to populate the Spinner-2 on basis of Spinner-1 .Like if I select Newton then only Force and Apple not the other values and if I select Hiiii value then only ds should populate on the Spinner-2 like wise.
My below code successfully populate the values from the xml url IN BOTH THE spinner-1 and SPINNER-2 but not with the particular way the values which i showed in the above image.
title = new ArrayList<String>(); title2 = new ArrayList<String>(); button = (Button) findViewById(R.id.button1); spinner = (Spinner) findViewById(R.id.spinner1); spinner.setOnItemSelectedListener(this); spinner2 = (Spinner) findViewById(R.id.spinner2); spinner2.setOnItemSelectedListener(this); dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US); findViewsById(); setDateTimeField(); if (android.os.Build.VERSION.SDK_INT > 9) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); } button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub parse(); from_adapter=new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_spinner_item, title); from_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); from_adapter2=new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_spinner_item, title2); from_adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(from_adapter); spinner2.setAdapter(from_adapter2); } private Object from_adapter(int i) { // TODO Auto-generated method stub return null; } }); } public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { Toast.makeText(parent.getContext(), ""+spinner.getSelectedItem().toString().trim(), Toast.LENGTH_LONG).show(); } public void onNothingSelected(AdapterView<?> arg0) { } protected void parse() { // TODO Auto-generated method stub try { URL url = new URL( "URL Link"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new InputSource(url.openStream())); doc.getDocumentElement().normalize(); NodeList nodeList = doc.getElementsByTagName("AAAA"); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); Element fstElmnt = (Element) node; NodeList nameList = fstElmnt.getElementsByTagName("a:W"); Element nameElement = (Element) nameList.item(0); nameList = nameElement.getChildNodes(); title.add(((Node) nameList.item(0)).getNodeValue()); } NodeList nodeList2 = doc.getElementsByTagName("a:AAAA"); for (int i = 0; i < nodeList2.getLength(); i++) { Node node = nodeList2.item(i); Element fstElmnt = (Element) node; NodeList nameList = fstElmnt.getElementsByTagName("a:R"); Element nameElement = (Element) nameList.item(0); nameList = nameElement.getChildNodes(); title2.add(((Node) nameList.item(0)).getNodeValue()); } Set<String> set = new LinkedHashSet<String>(title); title = new ArrayList<String>(set); Set<String> set2 = new LinkedHashSet<String>(title2); title2 = new ArrayList<String>(set2); } catch (Exception e) { System.out.println("XML Pasing Excpetion = " + e); } }
No comments:
Post a Comment