在android中的Spinner中显示默认值

时间:2023-01-21 14:52:31

I want to show a dropdown for gender selection. I passed a string array as

我想显示性别选择的下拉列表。我传递了一个字符串数组

String arr[]=new String[]{"male","female"};

but the problem is that is shows default selection with the value of "male" and I want to show "Gender" as default value. If I pass "Gender" in array at position 0, then it is visible in dropdown also. I just want "Gender" as hint but it must not be shown in dropdown.

但问题是显示默认选择的值为“男性”,我想将“性别”显示为默认值。如果我在位置0的数组中传递“Gender”,那么它也会在下拉列表中显示。我只想要“性别”作为提示,但不能在下拉列表中显示。

Can anybody tell me how I can do this. Thanks in advance.

任何人都可以告诉我如何做到这一点。提前致谢。

5 个解决方案

#1


-10  

You can set prompt to the spinner ... that can be set in the xml with android:prompt="Gender"

你可以设置spinner的提示...可以在xml中设置android:prompt =“Gender”

#2


51  

Spinner sp = (Spinner)findViewById(R.id.spinner); 
sp.setSelection(pos);

here pos is integer (your array item position)

这里pos是整数(你的数组项位置)

array is like below then pos = 0;

数组如下,则pos = 0;

String str[] = new String{"Select Gender","male", "female" };

then in onItemSelected

然后在onItemSelected中

@Override
    public void onItemSelected(AdapterView<?> main, View view, int position,
            long Id) {

        if(position > 0){
          // get spinner value
        }else{
          // show toast select gender
        }

    }

#3


1  

Spinner don't support Hint, i recommend you to make a custom spinner adapter.

Spinner不支持提示,我建议你制作一个自定义的微调器适配器。

check this link : https://*.com/a/13878692/1725748

检查此链接:https://*.com/a/13878692/1725748

#4


0  

I found a solution by extending ArrayAdapter and Overriding the getView method.

我通过扩展ArrayAdapter和覆盖getView方法找到了解决方案。

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

/**
 * A SpinnerAdapter which does not show the value of the initial selection initially,
 * but an initialText.
 * To use the spinner with initial selection instead call notifyDataSetChanged().
 */
public class SpinnerAdapterWithInitialText<T> extends ArrayAdapter<T> {

    private Context context;
    private int resource;

    private boolean initialTextWasShown = false;
    private String initialText = "Please select";

    /**
     * Constructor
     *
     * @param context The current context.
     * @param resource The resource ID for a layout file containing a TextView to use when
     *                 instantiating views.
     * @param objects The objects to represent in the ListView.
     */
    public SpinnerAdapterWithInitialText(@NonNull Context context, int resource, @NonNull T[] objects) {
        super(context, resource, objects);
        this.context = context;
        this.resource = resource;
    }

    /**
     * Returns whether the user has selected a spinner item, or if still the initial text is shown.
     * @param spinner The spinner the SpinnerAdapterWithInitialText is assigned to.
     * @return true if the user has selected a spinner item, false if not.
     */
    public boolean selectionMade(Spinner spinner) {
        return !((TextView)spinner.getSelectedView()).getText().toString().equals(initialText);
    }

    /**
     * Returns a TextView with the initialText the first time getView is called.
     * So the Spinner has an initialText which does not represent the selected item.
     * To use the spinner with initial selection instead call notifyDataSetChanged(),
     * after assigning the SpinnerAdapterWithInitialText.
     */
    @Override
    public View getView(int position, View recycle, ViewGroup container) {
        if(initialTextWasShown) {
            return super.getView(position, recycle, container);
        } else {
            initialTextWasShown = true;
            LayoutInflater inflater = LayoutInflater.from(context);
            final View view = inflater.inflate(resource, container, false);

            ((TextView) view).setText(initialText);

            return view;
        }
    }
}

What Android does when initialising the Spinner, is calling getView for the selected item before calling getView for all items in T[] objects. The SpinnerAdapterWithInitialText returns a TextView with the initialText, the first time it is called. All the other times it calls super.getView which is the getView method of ArrayAdapter which is called if you are using the Spinner normally.

Android初始化Spinner时所做的是在为T []对象中的所有项调用getView之前调用所选项的getView。 SpinnerAdapterWithInitialText在第一次调用时返回带有initialText的TextView。所有其他时候它调用super.getView,它是ArrayAdapter的getView方法,如果您正常使用Spinner,则会调用它。

To find out whether the user has selected a spinner item, or if the spinner still displays the initialText, call selectionMade and hand over the spinner the adapter is assigned to.

要确定用户是否选择了微调器项,或者微调器仍然显示initialText,请调用selectionMade并移交适配器所指定的微调器。

#5


-5  

Try below:

试试以下:

   <Spinner
    android:id="@+id/YourSpinnerId"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:prompt="Gender" />

#1


-10  

You can set prompt to the spinner ... that can be set in the xml with android:prompt="Gender"

你可以设置spinner的提示...可以在xml中设置android:prompt =“Gender”

#2


51  

Spinner sp = (Spinner)findViewById(R.id.spinner); 
sp.setSelection(pos);

here pos is integer (your array item position)

这里pos是整数(你的数组项位置)

array is like below then pos = 0;

数组如下,则pos = 0;

String str[] = new String{"Select Gender","male", "female" };

then in onItemSelected

然后在onItemSelected中

@Override
    public void onItemSelected(AdapterView<?> main, View view, int position,
            long Id) {

        if(position > 0){
          // get spinner value
        }else{
          // show toast select gender
        }

    }

#3


1  

Spinner don't support Hint, i recommend you to make a custom spinner adapter.

Spinner不支持提示,我建议你制作一个自定义的微调器适配器。

check this link : https://*.com/a/13878692/1725748

检查此链接:https://*.com/a/13878692/1725748

#4


0  

I found a solution by extending ArrayAdapter and Overriding the getView method.

我通过扩展ArrayAdapter和覆盖getView方法找到了解决方案。

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

/**
 * A SpinnerAdapter which does not show the value of the initial selection initially,
 * but an initialText.
 * To use the spinner with initial selection instead call notifyDataSetChanged().
 */
public class SpinnerAdapterWithInitialText<T> extends ArrayAdapter<T> {

    private Context context;
    private int resource;

    private boolean initialTextWasShown = false;
    private String initialText = "Please select";

    /**
     * Constructor
     *
     * @param context The current context.
     * @param resource The resource ID for a layout file containing a TextView to use when
     *                 instantiating views.
     * @param objects The objects to represent in the ListView.
     */
    public SpinnerAdapterWithInitialText(@NonNull Context context, int resource, @NonNull T[] objects) {
        super(context, resource, objects);
        this.context = context;
        this.resource = resource;
    }

    /**
     * Returns whether the user has selected a spinner item, or if still the initial text is shown.
     * @param spinner The spinner the SpinnerAdapterWithInitialText is assigned to.
     * @return true if the user has selected a spinner item, false if not.
     */
    public boolean selectionMade(Spinner spinner) {
        return !((TextView)spinner.getSelectedView()).getText().toString().equals(initialText);
    }

    /**
     * Returns a TextView with the initialText the first time getView is called.
     * So the Spinner has an initialText which does not represent the selected item.
     * To use the spinner with initial selection instead call notifyDataSetChanged(),
     * after assigning the SpinnerAdapterWithInitialText.
     */
    @Override
    public View getView(int position, View recycle, ViewGroup container) {
        if(initialTextWasShown) {
            return super.getView(position, recycle, container);
        } else {
            initialTextWasShown = true;
            LayoutInflater inflater = LayoutInflater.from(context);
            final View view = inflater.inflate(resource, container, false);

            ((TextView) view).setText(initialText);

            return view;
        }
    }
}

What Android does when initialising the Spinner, is calling getView for the selected item before calling getView for all items in T[] objects. The SpinnerAdapterWithInitialText returns a TextView with the initialText, the first time it is called. All the other times it calls super.getView which is the getView method of ArrayAdapter which is called if you are using the Spinner normally.

Android初始化Spinner时所做的是在为T []对象中的所有项调用getView之前调用所选项的getView。 SpinnerAdapterWithInitialText在第一次调用时返回带有initialText的TextView。所有其他时候它调用super.getView,它是ArrayAdapter的getView方法,如果您正常使用Spinner,则会调用它。

To find out whether the user has selected a spinner item, or if the spinner still displays the initialText, call selectionMade and hand over the spinner the adapter is assigned to.

要确定用户是否选择了微调器项,或者微调器仍然显示initialText,请调用selectionMade并移交适配器所指定的微调器。

#5


-5  

Try below:

试试以下:

   <Spinner
    android:id="@+id/YourSpinnerId"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:prompt="Gender" />