微调选择定义为另一个微调的选择 [英] spinner selection defines choices for another spinner

查看:124
本文介绍了微调选择定义为另一个微调的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于纺纱的问题,并不能对如何做到这一点网上找到任何东西。

I have a question about spinners, and cannot find anything online about how to do it.

我在我的应用程序微调框,与地区的选择,即西南,东南。

I have a spinner in my app, with region selections i.e. Southwest, Southeast.

我要微调可用第二微调的选择依赖于在第一位的选择。即当用户选择西南第二微调的选择将是查尔斯湖,爱荷华州,阿瑟湖等。而当用户选择东南,第二微调的选择将是格雷特纳,新奥尔良,庐陵等。

I want the spinner to selections available of the second spinner to be dependent on the selection made in the first one. i.e. When the user selects Southwest the choices on the second spinner would be Lake Charles, Iowa, Lake Arthur etc. And when the user selects southeast, the choices of the second spinner would be Gretna, New Orleans, Luling etc.

我怎么去这样做?

例子是大大AP preciated!

examples would be greatly appreciated!

感谢您!

推荐答案

试试这个code ..

Try this Code..

我希望它会帮助你...

I hope it will helpful to you...

public class MainActivity extends Activity {

Spinner sp1,sp2;
ArrayAdapter<String> adp1,adp2;
List<String> l1,l2;
int pos;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    l1=new ArrayList<String>();

    l1.add("A");
    l1.add("B");

    sp1= (Spinner) findViewById(R.id.spinner1);
    sp2= (Spinner) findViewById(R.id.spinner2);

    adp1=new ArrayAdapter<String> (this,android.R.layout.simple_dropdown_item_1line,l1);
    adp1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    sp1.setAdapter(adp1);

    sp1.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub
            pos=arg2;
            add();

        }

        private void add() {
            // TODO Auto-generated method stub
            Toast.makeText(getBaseContext(), ""+pos, Toast.LENGTH_SHORT).show();

            switch(pos)
            {
            case 0:
                l2= new ArrayList<String>();                    
                l2.add("A 1");
                l2.add("A 2");

                adp2=new ArrayAdapter<String>(MainActivity.this,
                        android.R.layout.simple_dropdown_item_1line,l2);
                adp2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
                sp2.setAdapter(adp2);

                select();

                break;
            case 1:
                l2= new ArrayList<String>();                    
                l2.add("B 1");
                l2.add("B 2");

                adp2=new ArrayAdapter<String>(MainActivity.this,
                        android.R.layout.simple_dropdown_item_1line,l2);
                adp2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
                sp2.setAdapter(adp2);

                select();

                break;
            }

        }

        private void select() {
            // TODO Auto-generated method stub

            sp2.setOnItemSelectedListener(new OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getBaseContext(), "Test "+arg2, Toast.LENGTH_SHORT).show();

                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub

                }
            });

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });
    }

}

这篇关于微调选择定义为另一个微调的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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