如何将 hashmap KEY 传递给 spinner1 并将其相应的 VALUES 传递给 spinner2 android

How to pass hashmap KEY to spinner1 and its corresponding VALUES to spinner2 android

我正在从网络的 XML 值中获取值.....在下面的代码中我正确地获取了值但是我无法将 KEY 传递给 spinneer1 并且当 spinner1 被更改时它相应的 VALUES在 spinner2 中将相应更改。

在 LOGCAT 中,我得到了这样的值,我必须将其传递给两个旋转器

{
Jackfruit=[Cabbage, Kesar, Amond], 
Date=[van, hall, Brinjal], 
Brinjal=[Cabbage, Kesar, Amond]
}

所以,如果在 spinner1 中选择了 Jackfruit 那么- Cabbage, Kesar, Amond 应该在 spinner2

中显示

上面的logcat我是从MainActivity.java

的UPDATE-3的下面代码中得到的
NodeList nodeList = doc.getElementsByTagName("a:AAAAA");
for (int i = 0; i < nodeList.getLength(); i++) {
    Element el = (Element) nodeList.item(i);

    String awValue = el.getElementsByTagName("a:W").item(0).getTextContent();

    if (!values.containsKey(awValue)) {
        values.put(awValue, new ArrayList<String>());

    }
    String arValue = el.getElementsByTagName("a:R").item(0).getTextContent();
    values.get(awValue).add(arValue);
}
Log.d("ADebugTag", "Value: " + values);

而MainActivity.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, List<String>> values = new HashMap<String, List<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<?> 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(
                    "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);
        }

    }

如何在单击微调器时将键值传递给微调器 1 并将其相关值传递给微调器 2

变化-1

从下面的代码中,如果我更改了 spinner1 的值,那么它的对应值就会被正确烘烤,即如果选择了 Jackfruit,那么 Cabbage, Kesar, Amond 正确烘烤

public void onItemSelected(AdapterView<?> parent, View view, int pos,
            long id) {
        String lang_Name = parent.getItemAtPosition(pos).toString();
        List<String> lang_Key = values.get(lang_Name);
        Toast.makeText(parent.getContext(), ""+lang_Key,
                Toast.LENGTH_LONG).show();
    }

并且我希望根据 spinner1 动态地将烘烤值传递给 spinner2。

在 onItemSelected 中获得 spinner1 值(HashMap 键)并获得相应的值后,将获得的值提供给 spinner2 适配器,然后刷新它。

from_adapter2.notifyDataSetChanged();

试试这个代码;

public void onItemSelected(AdapterView<?> parent, View view, int pos,
        long id) {
    String lang_Name = parent.getItemAtPosition(pos).toString();
    List<String> lang_Key = values.get(lang_Name);
    from_adapter2.clear():
    for(String s : lang_Key){
          from_adapter2.insert(s, from_adapter2.getCount());
    }
    from_adapter2.notifyDataSetChanged();
}