android相关微调器

android相关微调器,android,Android,如何执行与android相关的微调器 我是这样做的: 私人纺纱机DDL国家/地区,DDL类别 @Override public void onCreate(Bundle savedInstanceState) { try { requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); setContentView(R.layout.pn

如何执行与android相关的微调器

我是这样做的:

私人纺纱机DDL国家/地区,DDL类别

@Override
public void onCreate(Bundle savedInstanceState) {
    try {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pnplandingpage);

        ddlCountry = (Spinner) findViewById(R.id.ddlCountry);
        ddlCategory = (Spinner) findViewById(R.id.ddlCategory);

        // BindCountry(ddlCountry);

        // ddlCountry
        // .setOnItemSelectedListener(new OnCountryItemSelectedListener());

        // ddlCategory
        // .setOnItemSelectedListener(new
        // OnCategoryItemSelectedListener());

        // ArrayAdapter<CharSequence> adapter = ArrayAdapter
        // .createFromResource(this, R.array.Country_array,
        // android.R.layout.simple_spinner_item);
        // adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // ddlCountry.setAdapter(adapter);

        ddlCountry.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View pview,
                    int pos, long id) {

                final String[] array = new String[] { "Category",
                        "5 Miles", "10 Miles", "15 Miles", "20 Miles",
                        "25 Miles", };

                List<String> list = new ArrayList<String>();
                for (int i = 0; i < array.length; i++) {
                    list.add(array[i]);
                }

                ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
                        PNPLandingPage.this,
                        android.R.layout.simple_spinner_item, list);
                dataAdapter
                        .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                ddlCategory.setAdapter(dataAdapter);

                Toast.makeText(
                        parent.getContext(),
                        "The Country is "
                                + parent.getItemAtPosition(pos).toString(),
                        Toast.LENGTH_LONG).show();
            }

            public void onNothingSelected(AdapterView parent) {
                // Do nothing.
                ddlCategory.setAdapter(null);

            }
        });

        // BindCategory(ddlCategory);

        // ddlCategory
        // .setOnItemSelectedListener(new OnCategoryItemSelectedListener());

        ddlCategory.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View pview,
                    int pos, long id) {

                final String[] array = new String[] { "Country",
                        "Country1", "Country2", "Country3", "Country4",
                        "Country5", };


                List<String> list = new ArrayList<String>();
                for (int i = 0; i < array.length; i++) {
                    list.add(array[i]);
                }

                ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
                        PNPLandingPage.this,
                        android.R.layout.simple_spinner_item, list);
                dataAdapter
                        .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                ddlCountry.setAdapter(dataAdapter);

                // ddlCategory
                // .setOnItemSelectedListener(new
                // OnCategoryItemSelectedListener());

                Toast.makeText(
                        parent.getContext(),
                        "The Category is "
                                + parent.getItemAtPosition(pos).toString(),
                        Toast.LENGTH_LONG).show();
            }

            public void onNothingSelected(AdapterView parent) {
                // Do nothing.
                ddlCountry.setAdapter(null);

            }
        });

    } catch (Exception ex) {
        Toast.makeText(getApplication(), "EXCEPTION :" + ex, 1000);
    }
}
@覆盖
创建时的公共void(Bundle savedInstanceState){
试一试{
requestWindowFeature(窗口。功能\u无\u标题);
super.onCreate(savedInstanceState);
setContentView(R.layout.pnplanningPage);
ddlCountry=(微调器)findViewById(R.id.ddlCountry);
ddlcegory=(微调器)findViewById(R.id.ddlcegory);
//宾德国家(DDL国家);
//国家
//.setOnItemSelectedListener(新的OnCountryItemSelectedListener());
//DDL类别
//.setOnItemSelectedListener(新建
//OnCategoryItemSelectedListener());
//ArrayAdapter适配器=ArrayAdapter
//.createFromResource(此,R.array.Country_数组,
//android.R.layout.simple\u微调器(项目);
//setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
//ddlCountry.setAdapter(适配器);
ddlCountry.setOnItemSelectedListener(新的OnItemSelectedListener(){
已选择公共视图(AdapterView父视图、View pview、,
内部位置,长id){
最终字符串[]数组=新字符串[]{“类别”,
“5英里”、“10英里”、“15英里”、“20英里”,
“25英里”};
列表=新的ArrayList();
for(int i=0;i
但是祝酒词每次都会被点燃,也就是说,被选中的祝酒词会被重复。我做错了什么我想问题是

当您将setOnItemSelectedListener()设置为任何微调器时,默认情况下会选中列表的第一项(或指定给它的数组),从而激发选定的onItemSelected()

在你的例子中,你的两个微调器都在启动mselected()方法,所以它们处于一种无限循环中

编辑答案

final String[] array = new String[]
        { "Country", "Country1", "Country2", "Country3", "Country4", "Country5", };

        List<String> list = new ArrayList<String>();
        for (int i = 0; i < array.length; i++)
        {
            list.add(array[i]);
        }

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(TwoSpinners.this, android.R.layout.simple_spinner_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        ddlCountry.setAdapter(dataAdapter);

        ddlCountry.setOnItemSelectedListener(new OnItemSelectedListener()
        {
            public void onItemSelected(AdapterView<?> parent, View pview, int pos, long id)
            {
                if (!parent.getItemAtPosition(pos).toString().equals("Country"))
                {
                    final String[] array = new String[]
                    { "Category", "5 Miles", "10 Miles", "15 Miles", "20 Miles", "25 Miles", };

                    List<String> list = new ArrayList<String>();
                    for (int i = 0; i < array.length; i++)
                    {
                        list.add(array[i]);
                    }

                    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(TwoSpinners.this, android.R.layout.simple_spinner_item, list);
                    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    ddlCategory.setAdapter(dataAdapter);

                    Toast.makeText(parent.getContext(), "The Country is " + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
                }
            }

            public void onNothingSelected(AdapterView parent)
            {
                // Do nothing.
                ddlCategory.setAdapter(null);

            }
        });

        // BindCategory(ddlCategory);

        // ddlCategory
        // .setOnItemSelectedListener(new OnCategoryItemSelectedListener());

        ddlCategory.setOnItemSelectedListener(new OnItemSelectedListener()
        {
            public void onItemSelected(AdapterView<?> parent, View pview, int pos, long id)
            {

                if (!parent.getItemAtPosition(pos).toString().equals("Category"))
                {

                    final String[] array = new String[]
                    { "Country", "Country1", "Country2", "Country3", "Country4", "Country5", };

                    List<String> list = new ArrayList<String>();
                    for (int i = 0; i < array.length; i++)
                    {
                        list.add(array[i]);
                    }

                    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(TwoSpinners.this, android.R.layout.simple_spinner_item, list);
                    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    ddlCountry.setAdapter(dataAdapter);

                    // ddlCategory
                    // .setOnItemSelectedListener(new
                    // OnCategoryItemSelectedListener());

                    Toast.makeText(parent.getContext(), "The Category is " + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
                }
            }

            public void onNothingSelected(AdapterView parent)
            {
                // Do nothing.
                ddlCountry.setAdapter(null);

            }
        });
final String[]数组=新字符串[]
{“国家”、“国家1”、“国家2”、“国家3”、“国家4”、“国家5”};
列表=新的ArrayList();
for(int i=0;i