我不能从微调器中选择

时间:2017-12-06 08:56:07

标签: android android-spinner

点击旋转器中的项目后没有选择任何内容。

    public  void getSpinnerItems(){


    StringRequest stringRequest = new StringRequest(Request.Method.POST,ToolsUtils.STOP_CODE_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {


                    try {


                        JSONArray jsonArray = new JSONArray(response);
                        JSONObject jsonObject=new JSONObject();
                        for(int i=0;i<jsonArray.length();i++) {
                             jsonObject=jsonArray.getJSONObject(i);
                            String s=jsonObject.getString("stopDesc");
                            eque_array.add(s);
                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    //displaying the error in toast if occurrs
                    error.printStackTrace();
                    Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });

//创建请求队列         RequestQueue requestQueue = Volley.newRequestQueue(this);

//将字符串请求添加到请求队列         requestQueue.add(stringRequest);

//填充微调器

    ArrayAdapter<String> arr=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,eque_array);
    spinner.setAdapter(arr);
}

我使用此侦听器返回所选项目

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

         String   selected_item =spinner.getItemAtPosition( position).toString();
            ((TextView) view).setTextColor(Color.RED);
           validator.setText(spinner);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            stop_code="nothing" ;
            validator.setText(spinner );
        }
    });

我点击了itme但它没有返回任何东西。

1 个答案:

答案 0 :(得分:0)

你可以试试这个 首先在布局xml中创建微调器,如

                    <Spinner
                        android:id="@+id/spinner"
                        android:layout_width="fill_parent"
                        android:layout_height="48dp"
                        android:background="@drawable/border"
                        android:dropDownVerticalOffset="50dp"
                        android:gravity="center_vertical"
                        android:padding="3dp"
                        android:prompt="@string/country_prompt"
                        android:spinnerMode="dropdown" />

create a spinner_items file in layout folder like  

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/spinnerItemStyle"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:layout_gravity="center"
    android:gravity="center"
    android:ellipsize="marquee"
    android:background="@drawable/spinner_item_divider"
    android:textAlignment="inherit"/>

并在收到回复后调用此方法,并根据您的要求进行更改

private void intializeAdapter(ArrayList<String>arrList) {

        final ArrayAdapter<String> spinnerArrayAdapter = new 
         ArrayAdapter<String>(this, R.layout.spinner_items, arrList) {
            @Override
            public boolean isEnabled(int position) {
                if (position == 0) {
                    return false;
                } else {

                    return true;
                }
            }

            @Override
            public View getDropDownView(int position, View convertView, ViewGroup parent) {
                View view = super.getDropDownView(position, convertView, parent);
                 tv = (TextView) view;
                TextView tv1 = (TextView) view;
                if (position == 0) {
                    // Set the hint text color gray
                    tv.setTextColor(Color.GRAY);

                } else {
                    tv.setTextColor(Color.BLACK);

                   // tv.setText(numberValue);


                    // tv.setBackgroundColor(Color.BLACK);
                }
                return view;
            }
        };
        spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_items);
        spinner.setAdapter(spinnerArrayAdapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String selectedItemText = (String) parent.getItemAtPosition(position);
                if (position > 0) {
                    result = selectedItemText;
                    textView=(TextView) view;
                    numberValue=selectedItemText;
                    if(numberValue.equals("other")){
                         openAlertDialog();
                    }else if(numberValue.equals("Andet")){
                        openAlertDialog();
                    }

                }
            }

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

            }
        });
    }