2017-05-31 182 views
-1

我的應用程序中有一個微調器,應該顯示一個項目列表。最初它會顯示一個默認文本,並且在選擇完成之後,所有的項目列表都會在下拉菜單中看到。微調問題是,當我選擇第一項時,默認值被選中。我知道一些,這可能是一個簡單的問題。但是,如果你打算倒戈,請在做之前建議一個答案,因爲我嘗試了所有可能的解決方案。下面我張貼我的代碼。請看一看。提前致謝。選擇微調器的第一項選擇默認值

The spinner Adapter: 

    public class CustomSpinnerAdapter extends ArrayAdapter<String> { 

    Context context; 
    ArrayList<String> objects; 
    String firstElement; 
    boolean isFirstTime; 

    public CustomSpinnerAdapter(Context context, int textViewResourceId, ArrayList<String> objects, String defaultText) { 
     super(context, textViewResourceId, objects); 
     this.context = context; 
     this.objects = objects; 
     this.isFirstTime = true; 
     setDefaultText(defaultText); 
    } 

    @Override 
    public View getDropDownView(int position, View convertView, ViewGroup parent) { 
     if(isFirstTime) { 
      objects.set(0, firstElement); 
      isFirstTime = false; 
     } 
     return getCustomDropdownView(position, convertView, parent); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     notifyDataSetChanged(); 
     return getCustomView(position, convertView, parent); 
    } 

    public void setDefaultText(String defaultText) { 
     this.firstElement = objects.get(0); 
     objects.set(0,defaultText); 
    } 

    public View getCustomView(int position, View convertView, ViewGroup parent) { 

     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View row = inflater.inflate(R.layout.spinner_row, parent, false); 
     TextView label = (TextView) row.findViewById(R.id.spinnerText); 
     label.setText(objects.get(position)); 

     return row; 
    } 
    public View getCustomDropdownView(int position, View convertView, ViewGroup parent) { 

     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View row = inflater.inflate(R.layout.simple_spinner_dropdown_item, parent, false); 
     CheckedTextView label = (CheckedTextView) row.findViewById(R.id.text1); 
     label.setText(objects.get(position)); 

     return row; 
    } 

} 

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

In the mainactivity: 

          CustomSpinnerAdapter customSpinnerAdapter = new CustomSpinnerAdapter((Context)MainActivity.this, 
           R.layout.spinner_row, dcuName, "Select One DCU"); 
         dcuListScheduler.setAdapter(customSpinnerAdapter); 
+0

其中是'setOnItemSelectedListener'方法嗎? –

+0

你意味着你必須防止選擇第一個項目在微調? –

回答

0

所選項目的檢查位置,如果是擦菜板則0,那麼它將返回true

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

        if (position>0) 
        { 
         return true; 

       } 

      } 

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

      } 
     }); 
0

你應該實現setOnItemSelectedListener方法。

dcuListScheduler.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 

       @Override 
       public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) 
       { 

        if(dcuListScheduler.getSelectedItem().toString().equals("Select One DCU")) 
         { 
         // Select Default 
         } 
        else 
         { 
         // Select Other Options 
         } 
       } 

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

       } 

      });