Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当用户未选择任何选项时,如何防止微调器选择第一项?_Android_Spinner - Fatal编程技术网

Android 当用户未选择任何选项时,如何防止微调器选择第一项?

Android 当用户未选择任何选项时,如何防止微调器选择第一项?,android,spinner,Android,Spinner,当我运行应用程序时,它将直接选择第一个项目,然后调用意图以GoogleMap,即使用户尚未选择任何选项。如何预防?下面是部分代码…谢谢 MainActivity.java 设置适配器时,请在MainActivity中使用此代码,它将帮助您。 SpinnerAdapter adapter = new SpinnerAdapter(this, countries) { @Override public boolean isEnabled(int position

当我运行应用程序时,它将直接选择第一个项目,然后调用意图以
GoogleMap
,即使用户尚未选择任何选项。如何预防?下面是部分代码…谢谢

MainActivity.java
设置适配器时,请在MainActivity中使用此代码,它将帮助您。

  SpinnerAdapter adapter = new SpinnerAdapter(this, countries) {

        @Override
        public boolean isEnabled(int position) {

            if (position == 0) {
                return false;
            }
            return true;

        }
    };
    customSpinner.setAdapter(adapter);
将此添加到您的代码中

国家/地区。添加(新国家/地区(“选择国家/地区”)

将其添加到适配器中

if (tempCountry.getCountryImage() !=  -1 ){
    image.setImageResource(tempCountry.getCountryImage());
    image.setVisibility(View.VISIBLE);
}else{
    image.setVisibility(View.GONE);
}
您的案例以案例1开头,请尝试下面的代码。。。
please try below code...

final ArrayList<Country> countries = new ArrayList<Country>();
    countries.add(new Country("Select Country", R.drawable.country_icon));
    countries.add(new Country("Malaysia", R.drawable.malaysia));
    countries.add(new Country("Korea", R.drawable.south_korea));
    countries.add(new Country("Argentina", R.drawable.argentina));
    countries.add(new Country("Australia", R.drawable.australia));
    countries.add(new Country("Japan", R.drawable.japan));
    countries.add(new Country("United Kingdom", R.drawable.united_kingdom));

    customSpinner = (Spinner)findViewById(R.id.custom_spinner);
    SpinnerAdapter adapter = new SpinnerAdapter(this, countries);
    customSpinner.setAdapter(adapter);
    customSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(Intent.ACTION_VIEW);

            switch (position) {
                case 0:
                break;
                case 1:
                    intent.setData(Uri.parse("geo:4.213155, 103.402914"));
                    break;
                case 2:
                    intent.setData(Uri.parse("geo:36.593562, 127.040436"));
                    break;
                case 3:
                    intent.setData(Uri.parse("geo:-34.883324, -65.140799"));
                    break;
                case 4:
                    intent.setData(Uri.parse("geo:-24.372645, 131.823709"));
                    break;
                case 5:
                    intent.setData(Uri.parse("geo:36.875761, 138.729092"));
                    break;
                case 6:
                    intent.setData(Uri.parse("geo:54.887410, -2.913750"));
                    break;
            }

            if (position!=0 && intent.resolveActivity(getPackageManager()) != null)
                    startActivity(intent);
            }
        }
最终ArrayList国家/地区=新ArrayList(); 添加(新国家(“选择国家”,R.drawable.Country_图标)); 添加(新国家(“马来西亚”,R.drawable.Malaysia)); 添加(新国家(“韩国”,R.drawable.韩国)); 添加(新国家(“阿根廷”,R.drawable.Argentina)); 添加(新国家(“澳大利亚”,R.drawable.Australia)); 添加(新国家(“日本”,R.drawable.Japan)); 添加(新国家(“联合王国”,R.drawable.联合王国)); customSpinner=(Spinner)findViewById(R.id.custom\u Spinner); SpinnerAdapter=新SpinnerAdapter(此,国家/地区); customSpinner.setAdapter(适配器); customSpinner.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){ @凌驾 已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){ 意向意向=新意向(意向.行动\视图); 开关(位置){ 案例0: 打破 案例1: setData(Uri.parse(“geo:4.213155103.402914”); 打破 案例2: setData(Uri.parse(“geo:36.593562127.040436”); 打破 案例3: intent.setData(Uri.parse(“geo:-34.883324,-65.140799”); 打破 案例4: intent.setData(Uri.parse(“geo:-24.372645131.823709”); 打破 案例5: setData(Uri.parse(“geo:36.875761138.729092”); 打破 案例6: setData(Uri.parse(“geo:54.887410,-2.913750”); 打破 } if(position!=0&&intent.resolveActivity(getPackageManager())!=null) 星触觉(意向); } }
您好,这可能有点晚了,但我有一个简单的解决方案,适合我。 我为任何微调器创建一个开关

所以我设置了一个变量
int喷丝头开关;
。 然后在
onCreate
中,我将开关设置为
spinnerSwitch=1;

然后

@覆盖
已选择公共空视图(AdapterView父视图、视图
,整数位置,长id){
String itemText=parent.getItemAtPosition(position.toString();
Log.i(标记,“选定:”+itemText);
if(喷丝头开关==1){
喷丝头开关=0;
返回;
}
//然后您可以在这里编写代码
}

因此,当创建“我的活动”时,微调器会自动选择列表中的任何项目。我确信有许多奇特的解决方法,但这对我来说……目前是可行的。

您可以在此处找到解决方案。检查此项可能会帮助您:只需添加一个空元素或添加“选择国家”@oussemaroua如何创建“选择国家”若要从下拉列表中消失?我以前尝试过此操作,这将导致错误。因为开关案例应该从0开始,需要添加下面的代码,所以此方法可以工作:if(position==0)return;else{position-=1;}应该是案例0:return;因为如果break,它将调用Intent.ACTION\u VIEW right?但是“select country”仍在下拉列表中…如何删除它?您可以通过
countries来删除它。删除(0);适配器。notifyDataSetChanged()
我刚刚尝试了此方法,但效果很好!但需要将null更改为0,因为图像资源ID是一个整数值..感谢这是真的我的错误,抱歉实际添加位置:countries.remove(0);adapter.notifyDataSetChanged()…我尝试插入,但也失败了
if (tempCountry.getCountryImage() !=  -1 ){
    image.setImageResource(tempCountry.getCountryImage());
    image.setVisibility(View.VISIBLE);
}else{
    image.setVisibility(View.GONE);
}
please try below code...

final ArrayList<Country> countries = new ArrayList<Country>();
    countries.add(new Country("Select Country", R.drawable.country_icon));
    countries.add(new Country("Malaysia", R.drawable.malaysia));
    countries.add(new Country("Korea", R.drawable.south_korea));
    countries.add(new Country("Argentina", R.drawable.argentina));
    countries.add(new Country("Australia", R.drawable.australia));
    countries.add(new Country("Japan", R.drawable.japan));
    countries.add(new Country("United Kingdom", R.drawable.united_kingdom));

    customSpinner = (Spinner)findViewById(R.id.custom_spinner);
    SpinnerAdapter adapter = new SpinnerAdapter(this, countries);
    customSpinner.setAdapter(adapter);
    customSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(Intent.ACTION_VIEW);

            switch (position) {
                case 0:
                break;
                case 1:
                    intent.setData(Uri.parse("geo:4.213155, 103.402914"));
                    break;
                case 2:
                    intent.setData(Uri.parse("geo:36.593562, 127.040436"));
                    break;
                case 3:
                    intent.setData(Uri.parse("geo:-34.883324, -65.140799"));
                    break;
                case 4:
                    intent.setData(Uri.parse("geo:-24.372645, 131.823709"));
                    break;
                case 5:
                    intent.setData(Uri.parse("geo:36.875761, 138.729092"));
                    break;
                case 6:
                    intent.setData(Uri.parse("geo:54.887410, -2.913750"));
                    break;
            }

            if (position!=0 && intent.resolveActivity(getPackageManager()) != null)
                    startActivity(intent);
            }
        }
@Override
        public void onItemSelected(AdapterView<?> parent, View view
                , int position, long id) {
            String itemText = parent.getItemAtPosition(position).toString();
            Log.i(TAG, "Selected: " + itemText );
            if(spinnerCitySwitch == 1){
                spinnerCitySwitch = 0;
                return;
            }
            //Then you can write your code here
             
        }