Spinner getSelectedItem()返回worng位置

时间:2018-01-02 12:29:51

标签: java android

使用

时出错

<?php include_once("db.php"); $result=array(); $response=array(); if(isset($_POST['keyword']) && !empty($_POST['keyword'])) { $keyword = $_POST['keyword']; $sql = "SELECT * FROM search WHERE CONCAT(designation,' ', district, ' ', department, ' ',name) LIKE ?"; $stmt=$db->prepare($sql); $key = "%".$keyword."%"; $stmt->bind_param("s",$key); $stmt->execute(); $d=$stmt->get_result(); while($b=$d->fetch_array(MYSQLI_ASSOC)){ $result[]=$b; } $response['result']=$result; echo json_encode($response); } else { echo json_encode(array("message"=>"Keyword is not defined")); } ?>

日志是:

String a=(String)spinner.getSelectedItem();

但是我的微调器列表大小是7,请帮助我。

3 个答案:

答案 0 :(得分:0)

试试这个

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
    // your code here
 String selectedValue=arralist.get(position);
}

@Override
public void onNothingSelected(AdapterView<?> parentView) {
    // your code here
}

});

答案 1 :(得分:0)

Spinner spinner = (Spinner)findViewById(R.id.spinner);

String text = spinner.getSelectedItem()。toString();

使用此

答案 2 :(得分:0)

旋转器位置从0开始,旋转器采用默认选择位置。

你需要停止微调器选择默认选择位置只需使用布尔函数

刚刚发起private boolean isSpinnerInitial = true;

创建方法内部

 spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {


            if (isSpinnerInitial) {
                    isSpinnerInitial = false;
                } else {
              String position = String.valueOf(spinner.getSelectedItemPosition());
             }

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

        }
    });