如何在用户未选择任何选项时阻止微调器选择第一个项目?

时间:2017-07-15 08:42:05

标签: android spinner

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

MainActivity.java

final ArrayList<Country> countries = new ArrayList<Country>();
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:
                intent.setData(Uri.parse("geo:4.213155, 103.402914"));
                break;
            case 1:
                intent.setData(Uri.parse("geo:36.593562, 127.040436"));
                break;
            case 2:
                intent.setData(Uri.parse("geo:-34.883324, -65.140799"));
                break;
            case 3:
                intent.setData(Uri.parse("geo:-24.372645, 131.823709"));
                break;
            case 4:
                intent.setData(Uri.parse("geo:36.875761, 138.729092"));
                break;
            case 5:
                intent.setData(Uri.parse("geo:54.887410, -2.913750"));
                break;
        }

        if (intent.resolveActivity(getPackageManager()) != null)
                startActivity(intent);
        }
    }

SpinnerAdapter.java

public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View spinnerItem = convertView;

        if(spinnerItem == null){
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(getContext().LAYOUT_INFLATER_SERVICE);
            spinnerItem = inflater.inflate(R.layout.spinner_rows, parent, false);
            //spinnerItem = LayoutInflater.from(getContext()).inflate(R.layout.spinner_rows, parent, false);
        }

        Country tempCountry = (Country) getItem(position);

        ImageView image = (ImageView) spinnerItem.findViewById(R.id.imageView);
        TextView text = (TextView) spinnerItem.findViewById(R.id.textView);

        image.setImageResource(tempCountry.getCountryImage());
        image.setVisibility(View.VISIBLE);
        text.setText(tempCountry.getCountryName());

        return spinnerItem;
    }

    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        return getView(position, convertView, parent);
    }

4 个答案:

答案 0 :(得分:1)

将此添加到您的代码

countries.add(new Country("Select country", -1));

将此添加到您的适配器

if (tempCountry.getCountryImage() !=  -1 ){
    image.setImageResource(tempCountry.getCountryImage());
    image.setVisibility(View.VISIBLE);
}else{
    image.setVisibility(View.GONE);
}

并且您的案件以case 1

开头

答案 1 :(得分:0)

请在您的MainActivity中使用此代码,同时设置适配器可以帮助您。

  SpinnerAdapter adapter = new SpinnerAdapter(this, countries) {

        @Override
        public boolean isEnabled(int position) {

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

        }
    };
    customSpinner.setAdapter(adapter);

答案 2 :(得分:0)

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

答案 3 :(得分:0)

嗨,这可能有点晚了,但我有一个对我有用的简单解决方案。 我为任何微调器创建了一个开关。

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

然后

@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
             
        }

因此,当我的活动创建时,微调器会自动选择列表中的任何项目。我确定有很多奇特的解决方法,但这对我有用......现在。