2015-09-15 54 views
0

如何設置在轉我的第一個值(默認值)爲空。意思是,直到用戶從列表中選擇值,微調器將沒有價值。我下面試過如何設置在轉我的第一個值(默認值)爲空

list.add(""); 

但是,它不起作用。以下是我的代碼:

if(values.contains("options")){ 
     spinner.setOnItemSelectedListener(MyClass.this); 
     List<String> list = new ArrayList<String>(); 
     list.add(""); 
     list.add("Test"); 
     list.add("Working"); 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item, list) {}; 

     adapter.setDropDownViewResource(R.layout.custom_spinner_list); 
     spinner.setAdapter(adapter);  
    } 
} 
+0

空的意思你要設置 – Pavya

+0

@ user3676184 - 空意味着「空」值或「無」字符串 – user45678

+0

沒有魯斯塔姆,我不想清除我的名單,我只是想微調的默認值是空的,意味着價值將只一旦用戶選擇顯示,直到這時微調將沒有任何價值 – user45678

回答

5

我找到了方法。下面是我們如何做到這一點,讓最後一行是

list.add(""); 

,並添加如下代碼:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item, list) { 

         @Override 
         public View getView(int position, View convertView, ViewGroup parent) { 

           View v = super.getView(position, convertView, parent); 
           if (position == getCount()) { 
            ((TextView)v.findViewById(android.R.id.text1)).setText(""); 
            ((TextView)v.findViewById(android.R.id.text1)).setHint(getItem(getCount())); //"Hint to be displayed" 
           } 

           return v; 
          }  

          @Override 
          public int getCount() { 
           return super.getCount()-1;   // you don't display last item. It is used as hint. 
          } 

        }; 


        adapter.setDropDownViewResource(R.layout.custom_spinner_list); 
        spinner.setAdapter(adapter); 
        spinner.setSelection(adapter.getCount());  //set the hint the default selection so it appears on launch. 
        spinner.setOnItemSelectedListener(this); 
        notifyDataSetChanged();   
+0

定義字符串項靜態微調setPrompt只能這是一個了不起的解決方案,因爲它避免了破解第一從不出現的觀點。我發佈了一個SpinnerNoDefault類,它將這個答案考慮在內。謝謝! –

0

由於以上user45678的回答,我寫了這個

public class SpinnerNoDefault extends Spinner { 

public void initialize(List<String> items) { 
    initialize(items, android.R.layout.simple_spinner_item, android.R.layout.simple_spinner_dropdown_item); 
} 

public void initialize(List<String> items, @LayoutRes int mItemResId, @LayoutRes int dropdownResId) { 
    List<String> itemsWithEmpty = new ArrayList<>(items); 
    itemsWithEmpty.add(""); 

    String[] choicesArray = itemsWithEmpty.toArray(new String[itemsWithEmpty.size()]); 
    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getContext(), mItemResId, choicesArray) { 
     @NonNull 
     @Override 
     public View getView(int position, View convertView, @NonNull ViewGroup parent) { 
      View v = super.getView(position, convertView, parent); 
      if (position == getCount()) { 
       ((TextView)v.findViewById(android.R.id.text1)).setText(null); 
       ((TextView)v.findViewById(android.R.id.text1)).setHint(parent.getContext().getString(R.string.spinner_no_default_hint)); 
      } 

      return v; 
     } 

     @Override 
     public int getCount() { 
      // Don't display last item. It is used as hint. 
      return super.getCount()-1; 
     } 
    }; 

    spinnerArrayAdapter.setDropDownViewResource(dropdownResId); 
    setAdapter(spinnerArrayAdapter); 
    setSelection(itemsWithEmpty.size()-1); 
} 
}