I have a Spinner
which gets populate using a SimpleCursorAdapter
. My cursor has some values, but i need the Spinner
to show an empty option by default.
我有一个Spinner,它使用SimpleCursorAdapter填充。我的光标有一些值,但是我需要微调器在默认情况下显示一个空选项。
I don't want to use ArrayAdapter<String>
, or CursorWrapper
in this app, for some reason.
由于某些原因,我不想在这个应用程序中使用ArrayAdapter
There should be a simpler way to show an empty option in the Spinner
by default.
在默认情况下,应该有一种更简单的方法在微调器中显示空选项。
4 个解决方案
#1
3
You can simply hide the unwanted view in the spinner adapter (getDropDownView ) :
您可以简单地将不想要的视图隐藏在spinner适配器(getDropDownView)中:
In my sample code, defaultposition is the position to hide (like a "Select value" position)
在我的示例代码中,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;
}
...
}
#2
2
Spinner
's OnItemSelectedListener
runs on the compile time as well that fetches the first item to view on the Spinner
selected item.
Spinner的OnItemSelectedListener也在编译时运行,它获取第一个要在Spinner选中的项上查看的项。
Add a dummy item (String - null " "
) on your SimpleCursorAdapter
and use spinner.setSelected(int thatSpecificPostionYouJustAdded)
.
在SimpleCursorAdapter上添加一个虚拟项(String - null ")并使用微调器。setSelected(int thatSpecificPostionYouJustAdded)。
#3
2
A method I sometimes use to add an extra record such as an "empty" option with a SimpleCursorAdapter destined for a Spinner is by using a UNION clause in my cursor query. EMPTY_SPINNER_STRING could be something like: "-- none specified --" or similar. Use an "order by" clause to get your empty record first and therefore the default value in the Spinner. A crude but effective way of getting the required result without changing the underlying table data. In my example I only want certain spinners to have a default empty value (those with a modifier type of "intensity".
我有时用来添加额外记录的方法是,在我的游标查询中使用UNION子句,例如“empty”选项,带有一个为微调器指定的SimpleCursorAdapter。EMPTY_SPINNER_STRING可以是:“——没有指定——”或类似的。使用“order by”子句首先获取空记录,因此是微调器中的默认值。一种简单但有效的方法,可以在不更改底层表数据的情况下获得所需的结果。在我的示例中,我只希望某些spinners具有默认的空值(具有修饰符类型的“强度”)。
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;
}
}
#4
0
After setting the adapter. call setSelection (i used with 0) and right after that set the text color to transparent.
后设置适配器。调用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));
}
Then, set your OnItemSelectedListener (if needed).
然后,设置OnItemSelectedListener(如果需要)。
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
This will make the spinner empty at first time seen. But, if the user will select the first item it will do nothing because 0 is pre selected. For fixing this i used this subclass of spinner. taken from @melquiades's answer:
这将使转轮第一次看到空。但是,如果用户将选择第一个项目,它将不做任何事情,因为0是预先选择的。为了解决这个问题,我使用了spinner的子类。取自@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());
}
}
}
}
#1
3
You can simply hide the unwanted view in the spinner adapter (getDropDownView ) :
您可以简单地将不想要的视图隐藏在spinner适配器(getDropDownView)中:
In my sample code, defaultposition is the position to hide (like a "Select value" position)
在我的示例代码中,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;
}
...
}
#2
2
Spinner
's OnItemSelectedListener
runs on the compile time as well that fetches the first item to view on the Spinner
selected item.
Spinner的OnItemSelectedListener也在编译时运行,它获取第一个要在Spinner选中的项上查看的项。
Add a dummy item (String - null " "
) on your SimpleCursorAdapter
and use spinner.setSelected(int thatSpecificPostionYouJustAdded)
.
在SimpleCursorAdapter上添加一个虚拟项(String - null ")并使用微调器。setSelected(int thatSpecificPostionYouJustAdded)。
#3
2
A method I sometimes use to add an extra record such as an "empty" option with a SimpleCursorAdapter destined for a Spinner is by using a UNION clause in my cursor query. EMPTY_SPINNER_STRING could be something like: "-- none specified --" or similar. Use an "order by" clause to get your empty record first and therefore the default value in the Spinner. A crude but effective way of getting the required result without changing the underlying table data. In my example I only want certain spinners to have a default empty value (those with a modifier type of "intensity".
我有时用来添加额外记录的方法是,在我的游标查询中使用UNION子句,例如“empty”选项,带有一个为微调器指定的SimpleCursorAdapter。EMPTY_SPINNER_STRING可以是:“——没有指定——”或类似的。使用“order by”子句首先获取空记录,因此是微调器中的默认值。一种简单但有效的方法,可以在不更改底层表数据的情况下获得所需的结果。在我的示例中,我只希望某些spinners具有默认的空值(具有修饰符类型的“强度”)。
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;
}
}
#4
0
After setting the adapter. call setSelection (i used with 0) and right after that set the text color to transparent.
后设置适配器。调用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));
}
Then, set your OnItemSelectedListener (if needed).
然后,设置OnItemSelectedListener(如果需要)。
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
This will make the spinner empty at first time seen. But, if the user will select the first item it will do nothing because 0 is pre selected. For fixing this i used this subclass of spinner. taken from @melquiades's answer:
这将使转轮第一次看到空。但是,如果用户将选择第一个项目,它将不做任何事情,因为0是预先选择的。为了解决这个问题,我使用了spinner的子类。取自@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());
}
}
}
}