我有一個Spinner
,它使用SimpleCursorAdapter
填充。我的光標有一些值,但我需要Spinner
默認顯示空白選項。使用空默認選擇的微調器
出於某種原因,我不想在此應用中使用ArrayAdapter<String>
或CursorWrapper
。
默認情況下,應該有一種更簡單的方式在Spinner
中顯示空白選項。
我有一個Spinner
,它使用SimpleCursorAdapter
填充。我的光標有一些值,但我需要Spinner
默認顯示空白選項。使用空默認選擇的微調器
出於某種原因,我不想在此應用中使用ArrayAdapter<String>
或CursorWrapper
。
默認情況下,應該有一種更簡單的方式在Spinner
中顯示空白選項。
Spinner
的OnItemSelectedListener
在編譯時運行,並獲取第一個項目以查看Spinner
選定項目。
在您的SimpleCursorAdapter
上添加虛擬物品(String - null " "
)並使用spinner.setSelected(int thatSpecificPostionYouJustAdded)
。
你可以簡單地隱藏在微調適配器(getDropDownView)不需要的觀點:
在我的示例代碼,defaultposition是隱藏的位置(如「選擇價值」的位置)
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;
}
...
}
我明白了。但是你必須有一個空系統,我沒有在我的數據庫中。這是真正的問題。 – AAP 2012-04-29 18:56:31
我有時使用一個方法來添加一個額外的記錄,例如一個SimpleCursorAdapter指向一個Spinner的「空白」選項,在我的遊標查詢中使用了UNION子句。 EMPTY_SPINNER_STRING可能是這樣的:「 - 沒有指定 - 」或類似。使用「order by」子句首先獲取空記錄,並因此獲得Spinner中的默認值。在不改變基礎表數據的情況下獲得所需結果的一種簡單而有效的方式。在我的例子,我只希望某些紡紗廠有一個默認爲空值(那些具有修飾型「強度」的。
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;
}
}
設置適配器之後,調用爲setSelection(我用0)和集之後文字顏色爲透明。
// 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));
}
然後,設置您的OnItemSelectedListener(如果需要)。
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
這將使微調空在看到第一次。 但是,如果用戶將選擇第一個項目,它將不會執行任何操作,因爲預先選擇了0。爲了解決這個問題,我使用了Spinner的這個子類。從@梅爾基亞德斯的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());
}
}
}
}
一個ArrayAdapter有一個 '添加' 方法,但SimpleCursorAdapter haven't它。你能解釋一下如何在不使用CursorWrapper的情況下爲代碼添加一個空值?謝謝 –
AAP
2012-01-30 13:52:15
對不起,遲到了,可能[這](http://stackoverflow.com/questions/8508678/how-to-add-an-item-to-simplecursoradapter)會有所幫助。 – IronBlossom 2012-01-31 07:04:08