如何为微调器设置“提示文本”?

时间:2016-06-14 11:23:12

标签: java android spinner android-spinner

数据库中的微调器的值。我很困惑为Spinner添加提示,我在SO中关注了一些帖子,但我对这个地方很震惊,我试过这个

    Spinner sp = (Spinner)findViewById(R.id.spinner); 
    sp.setSelection(pos);

该位置具有整数值,所以我将其作为位置的整数值,我也尝试使用setSeletction()方法中的getCount()

main.java

    //spinner for customer
        sp=(Spinner)findViewById(R.id.spinner);
        adapter=new ArrayAdapter<String>(this,R.layout.spinner_layout,R.id.txt,listItems);
        sp.setAdapter(adapter);
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sales_order);
        cus_name = (Spinner) findViewById(R.id.spinner);//customer spinner

        // Permission StrictMode
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        //spinner for customer
        sp=(Spinner)findViewById(R.id.spinner);
        adapter=new ArrayAdapter<String>(this,R.layout.spinner_layout,R.id.txt,listItems);
        sp.setAdapter(adapter);


    }

private class BackTask extends AsyncTask<Void,Void,Void> {
        ArrayList<String> list;
        protected void onPreExecute(){
            super.onPreExecute();
            list=new ArrayList<>();
        }
        protected Void doInBackground(Void...params){
            InputStream is=null;
            String result="";
            try{
                HttpClient httpclient=new DefaultHttpClient();
                HttpPost httppost= new HttpPost("http://IP/web_services/customer_spinner.php");
                HttpResponse response=httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                // Get our response as a String.
                is = entity.getContent();
            }catch(IOException e){
                e.printStackTrace();
            }

            //convert response to string
            try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
                String line = null;
                while ((line = reader.readLine()) != null) {
                    result+=line;
                }
                is.close();
                //result=sb.toString();
            }catch(Exception e){
                e.printStackTrace();
            }
            // parse json data
            try{
                JSONObject object = new JSONObject(result);
                JSONArray jArray = object.getJSONArray("cus_Name");
                //JSONArray jArray =new JSONArray(result);
                for(int i=0;i<jArray.length();i++){
                    JSONObject jsonObject=jArray.getJSONObject(i);
                    // add interviewee name to arraylist
                    list.add(jsonObject.getString("cus_Name"));
                }
            }
            catch(JSONException e){
                e.printStackTrace();
            }
            return null;
        }
        protected void onPostExecute(Void result){
            listItems.addAll(list);
            adapter.notifyDataSetChanged();
        }
    }

1 个答案:

答案 0 :(得分:4)

有很多方法:

1。) Android Hint Spinner

在build.gradle文件中使用此依赖项。

dependencies {
    compile 'me.srodrigo:androidhintspinner:1.0.0'
}

输出

enter image description here

有关详细信息,请访问:https://github.com/srodrigo/Android-Hint-Spinner

2.)第二种方式

访问:  https://github.com/ravivyas84/AndroidSpinnerHint