具有空默认选择的微调器

时间:2012-01-28 19:56:16

标签: android eclipse android-spinner

我有Spinner使用SimpleCursorAdapter进行填充。我的光标有一些值,但我需要Spinner默认显示一个空选项。

出于某种原因,我不想在此应用中使用ArrayAdapter<String>CursorWrapper

默认情况下,应该有一种更简单的方法在Spinner中显示空选项。

5 个答案:

答案 0 :(得分:4)

您可以在spinner适配器(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;
}


 ...
}

答案 1 :(得分:2)

Spinner的{​​{1}}也会在编译时运行,它会抓取要在OnItemSelectedListener所选项目上查看的第一个项目。

Spinner上添加虚拟项目(String - null " ")并使用SimpleCursorAdapter

答案 2 :(得分:2)

我有时用来添加一个额外记录的方法,例如一个带有一个以Spinner为目标的SimpleCursorAdapter的“空”选项,就是在我的游标查询中使用一个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;
    }
}

答案 3 :(得分:0)

设置适配器后。调用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是预先选择的。为了解决这个问题,我使用了这个微调器的子类。取自@ melquiades&#39; 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());
            }
        }
    }
}

答案 4 :(得分:0)

创建一个NullSpinnerItem类,并将其插入列表的开头。

// Class to represent the `null` selection in a List of items in a Spinner.
// There is no easy way to tell Spinner to also include a blank or null value. 
// This allows us to inject this as the first item in the List and handle null values easily.
//
public class NullSpinnerItem {

  @Override
  public String toString() {
    return "None";
  }

}

然后,当您填充微调器时,只需获取项目,然后将其添加到第一个位置:

items.add( 0, new NullSpinnerItem() ); // items are your items.

ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource( R.layout.spinner_list_item);

Spinner spinner = (Spinner) findViewById(spinnerId);
spinner.setAdapter(adapter);

微调器中显示的是toString()方法。