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

时间:2017-05-31 06:00:56

标签: android android-spinner

我的应用程序中有一个微调器,它应该显示一个项目列表。最初它将显示默认文本,并且在选择完成后,将在下拉菜单中看到所有项目列表。微调器的问题是,当我选择第一个项目时,默认值将被选中。我知道,这可能是一个简单的问题。但是,如果你打算投票,请在做之前建议一个答案,因为我已经尝试了所有可能的解决方案。下面我发布我的代码。请看一看。提前谢谢。

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

}

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

In the mainactivity:

                            CustomSpinnerAdapter customSpinnerAdapter = new CustomSpinnerAdapter((Context)MainActivity.this,
                                R.layout.spinner_row, dcuName, "Select One DCU");
                        dcuListScheduler.setAdapter(customSpinnerAdapter);

2 个答案:

答案 0 :(得分:1)

您应该实施 setOnItemSelectedListener 方法。

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) {

                }

            });

答案 1 :(得分:0)

检查所选项目的位置,如果它是更大的,那么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) {

            }
        });