微调器没有选择第一项

时间:2016-07-12 10:12:01

标签: android android-spinner

这是我的代码

$n = $_GET['user'];


// Prepare the statement
if ($sql = $mysqli->prepare("SELECT password FROM tbl_login WHERE name=?")) {


// Bind a variable to the parameter as a string. 
$sql->bind_param("s", $n);

// Execute the statement.
$sql->execute();

// Get the variables from the query.
$sql->bind_result($pass);

// Fetch the data.
$sql->fetch();


// Close the prepared statement.
$sql->close();
}

设置微调器适配器 typeSpinnerAdapterForAge

Spinner agegroup;

 public void generateAgeGroupSpinner() {
        final List<AgeGroupModel> ageGroup = new ArrayList<>();
        agegroup = (Spinner) findViewById(R.id.ageGroupSpinner);
        // Spinner click listener
        agegroup
                .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent,
                                               View view, int position, long id) {
                        // On selecting a spinner item
                        String item = parent.getItemAtPosition(position)
                                .toString();

                    }

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

                    }
                });
        // Spinner Drop down elements
        ageGroupList.add(new AgeGroupModel(AgeGroup.BELOW18.getId(), AgeGroup.BELOW18.getName()));
        ageGroupList.add(new AgeGroupModel(AgeGroup.BETWEEN_19_35.getId(), AgeGroup.BETWEEN_19_35.getName()));
        ageGroupList.add(new AgeGroupModel(AgeGroup.BETWEEN_36_45.getId(), AgeGroup.BETWEEN_36_45.getName()));
        ageGroupList.add(new AgeGroupModel(AgeGroup.ABOVE45.getId(), AgeGroup.ABOVE45.getName()));

        // Creating adapter for spinner
        ArrayAdapter<AgeGroupModel> agedataAdapter = new ArrayAdapter<AgeGroupModel>(
                this, android.R.layout.simple_spinner_item, ageGroup);

        // Drop down layout style - list view with radio button
        agedataAdapter
                .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner

        agegroup.setAdapter(typeSpinnerAdapterForAge);
        agegroup.setOnItemSelectedListener(typeSelectedListener);
        agegroup.setOnTouchListener(typeSpinnerTouchListener);
}

typeSpinnerSearchListener

private SpinnerAdapter typeSpinnerAdapterForAge = new BaseAdapter() {

        private TextView text;

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = mInflator.inflate(R.layout.row_spinner, null);
            }
            text = (TextView) convertView.findViewById(R.id.spinnerTarget);
            if (!selected) {
                if (parent.equals(agegroup)) {
                    text.setText(getText(R.string.age_group));
                }
            } else {
                text.setText(ageGroupList.get(position).getName());
            }
            return convertView;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public AgeGroupModel getItem(int position) {
            return ageGroupList.get(position);
        }

        @Override
        public int getCount() {
            return ageGroupList.size();
        }

        public View getDropDownView(int position, View convertView,
                                    ViewGroup parent) {
            if (convertView == null) {
                convertView = mInflator.inflate(
                        android.R.layout.simple_spinner_dropdown_item, null);
            }
            text = (TextView) convertView.findViewById(android.R.id.text1);
            text.setText(ageGroupList.get(position).getName());
            return convertView;
        }

        ;
    };

首先没有选择微调器下拉列表的第一个值, 需要选择其他项目,然后代码也可以在第一项上正常工作

1 个答案:

答案 0 :(得分:3)

默认情况下,在微调器中选择第一个项目,这就是为什么OnItemSelectListener没有再次调用,所以如果你想避免这种情况,那么你需要全局放置一个布尔变量,然后根据bool变量设置条件。我想通过这种方式你可以解决你的问题。