如何显示两个带有链接数据的微调框? [英] How to display two Spinners with linked data?

查看:82
本文介绍了如何显示两个带有链接数据的微调框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Spinner-1的基础上填充Spinner-2.

I want to populate the Spinner-2 on basis of Spinner-1 .

例如,如果我选择 Newton ,那么Spinner-2将仅显示 Force Apple ,而不显示其他值,如果我选择 Hiiii 只能在Spinner-2上填充 ds .这只是我在下图中显示的示例(所有值在原始应用程序中都是动态的).在spinner-1中选择了任何值的地方,应该在Spinner中填充带有确定标签的值-2.

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.

UPDATE-3 MainActivity.java

UPDATE-3 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);
        }

    }

更新4

我正在日志中获取以下值

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

因此,如果在 spinner1 中选择了波果,则-白菜,凯萨尔,阿蒙德应该在 spinner2 中显示

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

我从MainActivity.java UPDATE-3的以下代码获得以上日志猫

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);

如何将键值传递给Spinner,如果更改了Spinner键值,则应该在spinner2中更改相应的值

推荐答案

首次填充spinner1.为此,更改parse方法以返回主要标签列表(title1),并将其设置为spinner.

For the first time just fill spinner1. To do this change the parse method to return the list of main tags(title1),and set them to spinner.

然后在spinner1及其内部使用setOnItemSelectedListener,解析XML以查找与所选项目相关的项目,并基于该项目填充spinner2.您可以使用position来获得所选项目.为此,编写另一种方法(或使用相同的方法使用if条件来表示这不是主要标签),该方法将从spinner1中获取选定项作为输入并返回子项.然后将此列表添加到spinner2.

Then use setOnItemSelectedListener on spinner1 and inside that , parse your XML to find items related to the selected item and fill spinner2 based on that. You can get the selected item using it's position. To do this write another method(or use the same method using if condition to say this is not main tags) that get selected item from spinner1 as input and return sub items. Then add this list to spinner2.

最后,您必须具有以下内容:

Finally you must have something like this:

   spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position) 
       {           
           title2=parse(spinner1.get(position)
          //set title2 to spinner2
       });

这篇关于如何显示两个带有链接数据的微调框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆