Click here to Skip to main content
15,898,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<pre lang="java">

I want to populate the Spinner-2 on basis of Spinner-1 from hashmap key and its values to spinner and spinner2.

For example if I select Newton then Spinner-2 will show only Force and Apple and not the other values and if I select Hiiii only ds should populate on the Spinner-2. This is just an example which I am showing in the image below(all values are dynamic in the original app).Where ever there is any value selected in spinner-1 the value with the determined tag should be populated in the spinner-2.

Image Below-
http://i.stack.imgur.com/vxLEC.png

I also posted it in stackoverflow its link

http://stackoverflow.com/questions/36589263/how-to-display-two-spinners-with-linked-data

Java
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener, OnClickListener {

    ArrayList<string> title;
    ArrayList<string> title2;
    Button button;
    Spinner spinner;
    Spinner spinner2;
    private EditText fromDateEtxt;
    //private EditText toDateEtxt;

    private DatePickerDialog fromDatePickerDialog;
    //private DatePickerDialog toDatePickerDialog;

    private SimpleDateFormat dateFormatter;

    ArrayAdapter<string> from_adapter;
    ArrayAdapter<string> from_adapter2;
    Map<string,>> values = new HashMap<string,>>();
    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        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); 
        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            long id) {
        Toast.makeText(parent.getContext(), ""+spinner.getSelectedItem().toString().trim(),
                Toast.LENGTH_LONG).show();
    }

    public void onNothingSelected(AdapterView    }

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


        try {

            URL url = new URL(
                    "WEB URL");
            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++) {
                Element el = (Element) nodeList.item(i);
                // get the a:W value
                String awValue = el.getElementsByTagName("a:W").item(0).getTextContent();
                // add the above value in our map  as key if it isn't present in the map, this key will 
                // have a list associated with it in which ALL the values for a:R will be stored, if 
                // the awValue key is present then you just add the new a:R value to its list 
                if (!values.containsKey(awValue)) {
                    values.put(awValue, new ArrayList<string>());
                }
                // also add the value pointed by a:R to the list associated with a:W
                String arValue = el.getElementsByTagName("a:R").item(0).getTextContent();
                values.get(awValue).add(arValue);
                Log.d("ADebugTag", "Value: " + arValue);
            }
            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();         

                NodeList websiteList = fstElmnt.getElementsByTagName("a:R");
                Element websiteElement = (Element) websiteList.item(0);
                websiteList = websiteElement.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);
            // Collection<string> set = new LinkedHashSet<string>(months);
            Set<string> set2 = new LinkedHashSet<string>(title2);
            title2 = new ArrayList<string>(set2);
            System.out.println("list are");
            System.out.println(set);
            System.out.println("list 2 are");
            System.out.println(set2);
        } catch (Exception e) {
            System.out.println("XML Pasing Excpetion = " + e);
        }

    } 


I am Getting the below values IN THE LOGCAT

XML
{
Jackfruit=[Cabbage, Kesar, Amond], 
Datia=[Kalpi, Kesar, Brinjal], 
Brinjal=[Cabbage, Kesar, Amond], 
Etawah=[Potato, Kesar], 
Kalpi=[Cabbage, Amond]
}


So,if Jackfruit is selected in spinner1 then- Cabbage, Kesar, Amond should diplay in spinner2

The above logcat I am getting from the below code of the MainActivity.java


Java
NodeList nodeList = doc.getElementsByTagName("a:AAAAA");
            for (int i = 0; i < nodeList.getLength(); i++) {
                Element el = (Element) nodeList.item(i);
                // get the a:W value
                String awValue = el.getElementsByTagName("a:W").item(0).getTextContent();
                // add the above value in our map  as key if it isn't present in the map, this key will 
                // have a list associated with it in which ALL the values for a:R will be stored, if 
                // the awValue key is present then you just add the new a:R value to its list 
                if (!values.containsKey(awValue)) {
                    values.put(awValue, new ArrayList<String>());

                }
                // also add the value pointed by a:R to the list associated with a:W
                String arValue = el.getElementsByTagName("a:R").item(0).getTextContent();
                values.get(awValue).add(arValue);

               // Log.d("ADebugTag", "Value: " + arValue);
            }
             Log.d("ADebugTag", "Value: " +  values);


How should I pass the key value to spinner and if spinner key value is changed then respective values should change in spinner2
Posted
Updated 13-Apr-16 20:07pm
v8
Comments
Richard MacCutchan 14-Apr-16 3:23am    
You will need to pass the new values to spinner2, or send a message telling it to refresh the view from updated source.
Member 11128987 14-Apr-16 4:00am    
yaa that i understood will you please give any of the referral links
Richard MacCutchan 14-Apr-16 5:07am    
Why not go to the Android Developer site and take a look for yourself?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900