【问题标题】:How to prevent the spinner from selecting the first item when user does not select any option?当用户没有选择任何选项时,如何防止微调器选择第一项?
【发布时间】:2023-04-01 04:28:01
【问题描述】:

当我运行应用程序时,它会直接选择第一项,然后将意图调用到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);
    }

【问题讨论】:

标签: android spinner


【解决方案1】:

请在您的 MainActivity 中使用此代码,同时设置适配器会对您有所帮助。

  SpinnerAdapter adapter = new SpinnerAdapter(this, countries) {

        @Override
        public boolean isEnabled(int position) {

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

        }
    };
    customSpinner.setAdapter(adapter);

【讨论】:

    【解决方案2】:

    将此添加到您的代码中

    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开头

    【讨论】:

    • 但是“选择国家”仍在下拉列表中...如何摆脱它?
    • 可以通过countries.remove(0); adapter.notifyDataSetChanged()删除
    • 我刚才试了这个方法,效果很好!但是需要将null改为0,因为图片资源ID是整数值。。谢谢
    • 那是我的错对不起
    • 实际上在哪里添加这个:countries.remove(0); adapter.notifyDataSetChanged()...我尝试插入但也失败了
    【解决方案3】:
    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);
                }
            }
    

    【讨论】:

    • 我之前试过这个,这会导致错误。因为switch case应该从0开始
    • 需要在下面添加代码才能使用此方法: if(position == 0) return;否则{位置-= 1; }
    • 应该是 case 0: return; 因为如果 break 会调用 Intent.ACTION_VIEW 对吧?
    【解决方案4】:

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

    所以我设置了一个变量 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
                 
            }
    

    所以当我的活动被创建时,微调器会自动选择列表中的任何项目。我敢肯定那里有很多奇特的解决方法,但这对我有用……现在。

    【讨论】:

      【解决方案5】:

      MainActivity.java 中引入一个布尔变量boolean isFirstTime = true;onItemSelected() 方法中添加这一行:

      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
      //If the Spinner's item is selected for the first time then return and make 
      //isFirstTime to false, so when the user select it for the second time (as user is 
      //always second) the spinner will work fine.
                      if(isFirstTime){
                          isFirstTime = false;
                          return;
                      }
      ... }
      

      但是,当用户选择第一个选项时,android 不会调用 onItemSelected() 方法 因此,您必须制作一个自定义微调器,即使该项目被第二次选中,它也会调用 onItemSelected()

      public class CustomSpinner extends android.support.v7.widget.AppCompatSpinner {
      
          private int lastPosition = 0;
      
      //overrite the required constructors
      ...
      //
      
      //In android Spinners, when an item is selected for the second time then android does not call onItemSelected () method
      //However, when an item is selected, the setSelection method will always be triggered
      // So in this method, we record and check the last selection position. If they are the same, just call the monitor manually
          
      @Override
          public void setSelection(int position, boolean animate) {
              super.setSelection(position, animate);
              if (position == lastPosition){
                  getOnItemSelectedListener().onItemSelected(this,null,position,0);
              }
              lastPosition = position;
          }
      
          @Override
          public void setSelection(int position) {
              super.setSelection(position);
              if (position == lastPosition){
                  getOnItemSelectedListener().onItemSelected(this,null,position,0);
              }
              lastPosition = position;
          }
      }
      

      【讨论】:

        【解决方案6】:

        使列表的第一个选择为空。

        String selecteditem="";
         
        if (selecteditem.isEmpty())
        {
        
        }else{
            // do work
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-10-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多