Java 如何在从xml解析中选择的微调器上显示/TOAST字符串数组的相关项

Java 如何在从xml解析中选择的微调器上显示/TOAST字符串数组的相关项,java,android,xml,xml-parsing,Java,Android,Xml,Xml Parsing,如何Toast从微调器中选择的字符串值的相关值 我能够从下面的xml格式中获取微调器数据,但无法将其id传递给他们进行toast ** APIGetCitiesResponse xmlns=”http://xxxx"> 10 印度 30 土耳其 40 美国 10 印度 19 中国 40 美国 30 土耳其 10 印度 ** MainActivity.java public class MainActivity extends Activity implements AdapterView.O

如何Toast从微调器中选择的字符串值的相关值

我能够从下面的xml格式中获取微调器数据,但无法将其id传递给他们进行toast

**

APIGetCitiesResponse xmlns=”http://xxxx">
10
印度
30
土耳其
40
美国
10
印度
19
中国
40
美国
30
土耳其
10
印度
**

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);
        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);
                title2.clear();
                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);
                //from_adapter2.clear();
                //from_adapter2.notifyDataSetChanged();
                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) {
        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();
        Toast.makeText(parent.getContext(), ""+lang_Key,
                Toast.LENGTH_LONG).show();
    }

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

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


        try {

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

            NodeList nodeList = doc.getElementsByTagName("a:Pair");
            for (int i = 0; i < nodeList.getLength(); i++) {
                Element el = (Element) nodeList.item(i);
                // get the a:W value
                String awValue = el.getElementsByTagName("a:FromName").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:ToName").item(0).getTextContent();
                values.get(awValue).add(arValue);

               // Log.d("ADebugTag", "Value: " + arValue);
            }
             Log.d("ADebugTag", "Value: " +  values);
            for (int i = 0; i < nodeList.getLength(); i++) {

                Node node = nodeList.item(i);       

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

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

                title.add(((Node) nameList.item(0)).getNodeValue());

            }
            NodeList nodeList2 = doc.getElementsByTagName("a:Pair");
            for (int i = 0; i < nodeList2.getLength(); i++) {

                Node node = nodeList2.item(i);       

                Element fstElmnt = (Element) node;
                NodeList nameList = fstElmnt.getElementsByTagName("a:ToName");
                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);
        }

    }

    @Override
    public void onClick(View view) {
        if(view == fromDateEtxt) {
            fromDatePickerDialog.show();
        } else  {
            Log.d("errorrrr", "soryy");
        }

    }
}
public class City {
    int  id;
    String name;

    public City() {
    }

    public City(int id, String name) {
        this.id = id;
        this.name = name;
    }
    //getters ad setters
}
公共类MainActivity扩展活动实现AdapterView.OnItemSelectedListener、OnClickListener{
ArrayList标题;
ArrayList标题2;
按钮;
纺纱机;
喷丝器喷丝器2;
来自DateText的私有编辑文本;
//私有EditText-ToDateText;
来自DatePickerDialog的专用DatePickerDialog;
//专用日期选择器对话框到日期选择器对话框;
专用SimpleDataFormat日期格式化程序;
来自_适配器的阵列适配器;
来自_适配器2的阵列适配器;
映射值=新的HashMap();
@TargetApi(构建版本代码姜饼)
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
title=新的ArrayList();
title2=新的ArrayList();
按钮=(按钮)findViewById(R.id.button1);
微调器=(微调器)findViewById(R.id.spinner1);
spinner.setOnItemSelectedListener(此);
喷丝头2=(喷丝头)findViewById(R.id.spinner2);
//喷丝头2.SetonimSelectedListener(此);
dateFormatter=新的SimpleDataFormat(“dd-MM-yyyy”,Locale.US);
findViewsById();
setDateTimeField();
如果(android.os.Build.VERSION.SDK_INT>9){
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(策略);
}
setOnClickListener(新的OnClickListener()
{
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
parse();
from_adapter=new ArrayAdapter(getBaseContext(),android.R.layout.simple_微调器_项,标题);
来自\u adapter.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
标题2.clear();
from_adapter2=new ArrayAdapter(getBaseContext(),android.R.layout.simple_spinner_项,标题2);
来自_adapter2.setDropDownViewResource(android.R.layout.simple_微调器_下拉列表_项);
spinner.setAdapter(来自_适配器);
//从_适配器2.清除();
//来自_adapter2.notifyDataSetChanged();
喷丝头2.设置适配器(来自_适配器2);
}
来自_适配器的私有对象(int i){
//TODO自动生成的方法存根
返回null;
}
});
}
已选择公共视图(AdapterView父视图、视图、int pos、,
长id){
字符串lang_Name=parent.getItemAtPosition(pos.toString();
List lang\u Key=values.get(lang\u Name);
从_适配器2.清除();
for(字符串s:lang_键){
from_adapter2.insert(s,from_adapter2.getCount());
}
来自_adapter2.notifyDataSetChanged();
Toast.makeText(parent.getContext(),“”+lang_键,
Toast.LENGTH_LONG).show();
}
未选择公共无效(AdapterView arg0){
}
受保护的void parse(){
//TODO自动生成的方法存根
试一试{
URL=新URL(
“www.example.com”);
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
Document doc=db.parse(新的输入源(url.openStream());
doc.getDocumentElement().normalize();
NodeList NodeList=doc.getElementsByTagName(“a:Pair”);
for(int i=0;i NodeList nodeList = doc.getElementsByTagName("Pair");
    for (int i = 0; i < nodeList.getLength(); i++) {
        Element el = (Element) nodeList.item(i);

        String awValue = el.getElementsByTagName("FromName").item(0).getTextContent();
        if (!values.containsKey(awValue)) {
            values.put(awValue, new ArrayList<String>());
            String fromid = el.getElementsByTagName("FromID").item(0).getTextContent();
            Log.d("fromid fromid", "fromid: " +  fromid);

        }
        String arValue = el.getElementsByTagName("ToName").item(0).getTextContent();
        values.get(awValue).add(arValue);
    }
public class City {
    int  id;
    String name;

    public City() {
    }

    public City(int id, String name) {
        this.id = id;
        this.name = name;
    }
    //getters ad setters
}
Map<Integer, City> to = new HashMap<>();
Map<Integer, City> from = new HashMap<>();

protected void parse() {
    try {
        URL url = new URL("https://raw.githubusercontent.com/kushagrakhare/g/master/test.xml");
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new InputSource(url.openStream()));
        doc.getDocumentElement().normalize();

        NodeList nodeList = doc.getElementsByTagName("a:CityPair");
        for (int i = 0; i < nodeList.getLength(); i++) {
            Element el = (Element) nodeList.item(i);
            // get the a:W value
            String fromCityName = el.getElementsByTagName("a:FromCityName").item(0).getTextContent();
            int fromCityID = Integer.parseInt(el.getElementsByTagName("a:FromCityID").item(0).getTextContent());
            int toCityID = Integer.parseInt(el.getElementsByTagName("a:ToCityID").item(0).getTextContent());
            String toCityName = el.getElementsByTagName("a:ToCityName").item(0).getTextContent();

            City from1 = new City(fromCityID, fromCityName);
            City to1 = new City(toCityID, toCityName);
            from.put(fromCityID, from1);
            to.put(toCityID, to1);
        }

        for (City item : from.values()) {
            title.add(item.getName());
        }

        for (City item : to.values()) {
            title2.add(item.getName());
        }

    } catch (Exception e) {
        System.out.println("XML Pasing Excpetion = " + e);
    }

}
int spinner1SelectionPos;
int spinner2SelectionPos;
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    switch (parent.getId()) {
        case R.id.spinner1:
            spinner1SelectionPos = pos;
            break;
        case R.id.spinner2:
            spinner2SelectionPos = pos;
            break;
    }

    int fcityKey = getMapKey(from, title.get(spinner1SelectionPos));
    int tcityKey = getMapKey(to, title2.get(spinner2SelectionPos));

    String message = title.get(spinner1SelectionPos) + " " + fcityKey
            + " " + title2.get(spinner2SelectionPos) + " " + tcityKey;

    Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
}

//for getting key from a map using value
private int getMapKey(Map<Integer, City> map, String value) {
    for (Map.Entry<Integer, City> item : map.entrySet()) {
        if (item.getValue().getName().equals(value)) return item.getKey();
    }

    return -1;
}