13

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

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

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

4

5 回答 5

5

您可以简单地在微调器适配器 (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;
}


 ...
}
于 2012-04-28T09:01:06.670 回答
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;
    }
}
于 2013-03-01T05:57:39.437 回答
2

Spinner'sOnItemSelectedListener也在编译时运行,它会获取要在Spinner所选项目上查看的第一个项目。

在你的和使用上添加一个虚拟项目(String-null " ")。SimpleCursorAdapterspinner.setSelected(int thatSpecificPostionYouJustAdded)

于 2012-01-30T05:34:42.593 回答
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()方法是 Spinner 中显示的内容。

于 2019-05-28T02:13:28.797 回答
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 的回答


/** 
  * 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());
            }
        }
    }
}
于 2015-12-17T12:32:41.507 回答