2012-01-28 90 views
10

Tôi có một số Spinner được điền bằng cách sử dụng SimpleCursorAdapter. Con trỏ của tôi có một số giá trị, nhưng tôi cần Spinner để hiển thị tùy chọn trống theo mặc định.Spinner với lựa chọn mặc định trống

Tôi không muốn sử dụng ArrayAdapter<String> hoặc CursorWrapper trong ứng dụng này vì một số lý do.

Có cách đơn giản hơn để hiển thị tùy chọn trống trong Spinner theo mặc định.

Trả lời

2

Spinner 's OnItemSelectedListener cũng chạy trên thời gian biên dịch tìm nạp mục đầu tiên để xem trên mục được chọn Spinner.

Thêm mục giả (Chuỗi - null " ") trên số SimpleCursorAdapter và sử dụng spinner.setSelected(int thatSpecificPostionYouJustAdded).

+0

Một ArrayAdapter có một phương pháp 'thêm', nhưng SimpleCursorAdapter haven't nó. Bạn có thể giải thích làm thế nào thêm một giá trị null với mã, mà không cần sử dụng CursorWrapper ?. Cảm ơn – AAP

+0

Xin lỗi vì đã đến trễ, có thể [this] (http://stackoverflow.com/questions/8508678/how-to-add-an-item-to-simplecursoradapter) sẽ giúp bằng cách nào đó. – IronBlossom

3

Bạn chỉ có thể ẩn các quan điểm không mong muốn trong adapter spinner (getDropDownView):

Trong mã mẫu của tôi, defaultposition là vị trí để ẩn (như một vị trí "Chọn giá trị")

public class SpinnerOptionAdapter extends ArrayAdapter<optionsInfos> { 

... 

    @Override 

    public View getDropDownView(int position, View convertView, ViewGroup parent) 
    { // This view starts when we click the spinner. 
    View row = convertView; 
    if(row == null) 
    { 
     LayoutInflater inflater = context.getLayoutInflater(); 
     row = inflater.inflate(R.layout.product_tab_produit_spinner_layout, parent, false); 
    } 

    ... 

    optionsInfos item = data.get(position); 


    if((item != null) && (position == defaultposition)) { 
     row.setVisibility(View.GONE); 
    } else { 
     row.setVisibility(View.VISIBLE); 
    } 

    .... 

    return row; 
} 


... 
} 
+0

Tôi hiểu. Nhưng bạn phải có một nullem rỗng, và tôi đã không có nó trong db của tôi. đó là vấn đề thực sự. – AAP

2

Một phương pháp mà đôi khi tôi sử dụng để thêm một bản ghi phụ như một tùy chọn "trống" với SimpleCursorAdapter được đặt cho Spinner là sử dụng mệnh đề UNION trong truy vấn con trỏ của tôi. EMPTY_SPINNER_STRING có thể giống như sau: "- không được chỉ định -" hoặc tương tự. Sử dụng mệnh đề "order by" để lấy bản ghi trống của bạn trước và do đó giá trị mặc định trong Spinner. Một cách thô lỗ nhưng hiệu quả để nhận được kết quả cần thiết mà không thay đổi dữ liệu bảng bên dưới. Trong ví dụ của tôi, tôi chỉ muốn một số spinners nhất định có giá trị rỗng mặc định (những người có loại bổ trợ là "cường độ".

public Cursor getLOV(String modifier_type) 
//get the list of values (LOVS) for a given modifier 
{ 
    if (mDb == null) 
    { 
     this.open(); 
    } 
    try { 
     MYSQL = "SELECT _ID AS '_id', code, name, type as 'DESC', ordering FROM "+codeTab+" WHERE type = '"+modifier_type+"'" + 
       " ORDER BY ordering, LOWER(name)"; 
     if (modifier_type.equals("intensity")) { //then include a default empty record 
      MYSQL = "SELECT _ID AS '_id', code, name as 'NAME', type as 'DESC', ordering FROM "+codeTab+" WHERE type = '"+modifier_type+"'" + 
        " UNION SELECT 9999 AS '_id', '' AS 'code', '"+EMPTY_SPINNER_STRING+"' AS 'NAME', 'intensity' AS 'DESC', 1 AS ordering ORDER BY ordering, name"; 
     } 
     Log.d(TAG, "MYSQL = "+MYSQL); 
     return mDb.rawQuery(MYSQL, null); 
    } 
    catch (SQLiteException exception) { 
     Log.e("Database LOV query", exception.getLocalizedMessage()); 
     return null; 
    } 
} 
0

Sau khi thiết lập bộ điều hợp. màu sắc văn bản để minh bạch.

// Preselect the first to make the spinner text transparent 
    spinner.setSelection(0, false); 
    TextView selectedView = (TextView) spinner.getSelectedView(); 
    if (selectedView != null) { 
     selectedView.setTextColor(getResources().getColor(R.color.transparent)); 
    } 

Sau đó, thiết lập OnItemSelectedListener của bạn (nếu cần thiết).

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 

     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
     } 

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

này sẽ làm cho spinner trống ở lần đầu tiên nhìn thấy. Nhưng, nếu người dùng sẽ chọn mục đầu tiên, nó sẽ không làm gì vì 0 được chọn trước. Để sửa lỗi này tôi đã sử dụng phân lớp này của spinner. lấy từ @ melquiades của answer:


/** 
    * Spinner extension that calls onItemSelected even when the selection is the same as its previous value 
    */ 
public class FVRSpinner extends Spinner { 

    public FVRSpinner(Context context) { 
     super(context); 
    } 

    public FVRSpinner(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public FVRSpinner(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @Override 
    public void setSelection(int position, boolean animate) { 
     boolean sameSelected = position == getSelectedItemPosition(); 
     super.setSelection(position, animate); 
     if (sameSelected) { 
      // Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now 
      if (getOnItemSelectedListener() != null) { 
       getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId()); 
      } 
     } 
    } 

    @Override 
    public void setSelection(int position) { 
     boolean sameSelected = position == getSelectedItemPosition(); 
     super.setSelection(position); 
     if (sameSelected) { 
      // Spinner does not call the OnItemSelectedListener if the same item is selected, so do it manually now 
      if (getOnItemSelectedListener() != null) { 
       getOnItemSelectedListener().onItemSelected(this, getSelectedView(), position, getSelectedItemId()); 
      } 
     } 
    } 
}
Các vấn đề liên quan