2016-07-26 33 views
1

我正在使用以下代碼來選擇列表中的最後一項作爲微調控件的提示(即微調控件中的默認選定項目)並試圖從下拉菜單中隱藏它。在Spinner(默認項目)中使用項目作爲提示並將其隱藏在下拉列表中

List<String> rfpType = new ArrayList<>(); 
rfpType.add("Job"); 
rpType.add("Talent"); 
rfpType.add("Vendor"); 
rfpType.add("Sponsor"); 
rfpType.add("RFP Title"); 

HintAdapter dataAdapter1 = new HintAdapter(getActivity(), android.R.layout.simple_list_item_1, rfpType); 
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
spinnerRFPType.setAdapter(dataAdapter1); 
spinnerRFPType.setSelection(dataAdapter1.getCount()); 

HintAdapter

class HintAdapter extends ArrayAdapter<String>{ 

    public HintAdapter(Context context, int theLayoutResID , List<String> list){ 
     super(context, theLayoutResID, list); 
    } 

    @Override 
    public int getCount() { 
     // don't display last item. It is used as hint. 
     int count = super.getCount(); 
     return count > 0 ? count-1 : count; 
    } 

} 

但它顯示的倒數第二項爲默認值。並隱藏我想用作提示的最後一個項目。建議我一個正確的解決方案。

+0

變化spinnerRFPType.setSelection(dataAdapter1.getCount() - 1); spinnerRFPType.setSelection(dataAdapter1.getCount()); – Nisarg

+0

嘿它根據期望工作..我寫了相同的代碼,如上所述沒有change.check屏幕截圖.https://postimg.org/image/x15zi375j/ 和這一個 https://postimg.org/image/ jyacytgxj/ –

+0

@ShakeebAyaz我也認爲它應該工作。但不幸的是它沒有。任何想法如何糾正? – h8pathak

回答

0

將此android:prompt="@string/country_prompt"加到您的微調器中。

 <Spinner 
     android:id="@+id/spinner1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:entries="@array/country_arrays" 
     android:prompt="@string/country_prompt" /> 
+0

這不是我正在尋找的解決方案。此外,這需要一個線'android:spinnerMode =「對話框」'工作。 – TheHardRock

0

您需要實現以下方法在你的適配器類:

它會幫助你:

也爲getCount將返回計數不減少它

@Override 
public View getDropDownView(int position, View convertView, 
     ViewGroup parent) 
{ 
    LayoutInflater inflater = getLayoutInflater(null); 
    convertView = inflater.inflate(theLayoutResID, parent, 
      false); 
    convertView= null; 

    if(position == list.size() - 1) 
    { 
     holder.textView.setVisibility(View.GONE); 
     convertView= holder; 
    } 
    else{ 
     convertView= super.getDropDownView(position, null, parent); 
    } 
    return convertView; 

} 
相關問題