Android 选择微调器的第一项选择默认值

Android 选择微调器的第一项选择默认值,android,android-spinner,Android,Android Spinner,我的应用程序中有一个微调器,用来显示项目列表。最初,它将显示一个默认文本,选择后,所有项目列表将显示在下拉菜单中。微调器的问题是,每当我选择第一个项目时,默认值都会被选中。我知道对一些人来说,这可能是一个简单的问题。然而,如果你要投反对票,请在投票前提出一个答案,因为我已经尝试了所有可能的解决方案。下面是我的代码。请看一看。提前谢谢 The spinner Adapter: public class CustomSpinnerAdapter extends ArrayAdapter&l

我的应用程序中有一个微调器,用来显示项目列表。最初,它将显示一个默认文本,选择后,所有项目列表将显示在下拉菜单中。微调器的问题是,每当我选择第一个项目时,默认值都会被选中。我知道对一些人来说,这可能是一个简单的问题。然而,如果你要投反对票,请在投票前提出一个答案,因为我已经尝试了所有可能的解决方案。下面是我的代码。请看一看。提前谢谢

The spinner Adapter:

    public class CustomSpinnerAdapter extends ArrayAdapter<String> {

    Context context;
    ArrayList<String> objects;
    String firstElement;
    boolean isFirstTime;

    public CustomSpinnerAdapter(Context context, int textViewResourceId, ArrayList<String> objects, String defaultText) {
        super(context, textViewResourceId, objects);
        this.context = context;
        this.objects = objects;
        this.isFirstTime = true;
        setDefaultText(defaultText);
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        if(isFirstTime) {
            objects.set(0, firstElement);
            isFirstTime = false;
        }
        return getCustomDropdownView(position, convertView, parent);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        notifyDataSetChanged();
        return getCustomView(position, convertView, parent);
    }

    public void setDefaultText(String defaultText) {
        this.firstElement = objects.get(0);
        objects.set(0,defaultText);
    }

    public View getCustomView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.spinner_row, parent, false);
        TextView label = (TextView) row.findViewById(R.id.spinnerText);
        label.setText(objects.get(position));

        return row;
    }
    public View getCustomDropdownView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.simple_spinner_dropdown_item, parent, false);
        CheckedTextView label = (CheckedTextView) row.findViewById(R.id.text1);
        label.setText(objects.get(position));

        return row;
    }

}

检查所选项目的位置,如果该项目大于0,则返回true

spinner_select_page.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                    if (position>0)
                    {
                       return true;

                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
spinner\u select\u page.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
如果(位置>0)
{
返回true;
}
}
@凌驾
未选择公共无效(AdapterView父级){
}
});

您应该实现方法

dcuListScheduler.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) 
                {

                    if(dcuListScheduler.getSelectedItem().toString().equals("Select One DCU"))
                      {
                        // Select Default
                      }
                    else
                      {
                         // Select Other Options 
                      }
                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {

                }

            });
dcuListScheduler.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共位置(适配器视图arg0、视图arg1、内部位置、长arg3)
{
if(dcuListScheduler.getSelectedItem().toString().equals(“选择一个DCU”))
{
//选择默认值
}
其他的
{
//选择其他选项
}
}
@凌驾
未选择公共无效(AdapterView arg0){
}
});

setOnItemSelectedListener方法在哪里?您的意思是必须防止在微调器上选择第一项?返回类型为void时无法返回值hahaha:鼃()()())@Hardikhttps://stackoverflow.com/users/8085378/hardik-kotadiya
dcuListScheduler.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) 
                {

                    if(dcuListScheduler.getSelectedItem().toString().equals("Select One DCU"))
                      {
                        // Select Default
                      }
                    else
                      {
                         // Select Other Options 
                      }
                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {

                }

            });