简体   繁体   中英

Android studio - How to make the first item in the spinner to be empty?

As the topic says, how can I make the first item to be empty?

As it is now, I have a value at the first place in the spinner that always is chosen from the beginning. This means that the application goes to the second activity directly, and one have no chance to select another items in the list.

I have saved all my Strings in string.xml, in a String array. The code in MainActivity looks like this;

spinner = (Spinner) findViewById(R.id.spinner);
adapter = ArrayAdapter.createFromResource(this, R.array.country_names, android.R.layout.simple_spinner_item);

adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);

spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        ((TextView) parent.getChildAt(0)).setText(null);
        ((TextView) parent.getChildAt(0)).setTextColor(Color.BLACK);
        ((TextView) parent.getChildAt(0)).setTextSize(23);

        Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
        String txtFromSpinner = mySpinner.getSelectedItem().toString();

        if (txtFromSpinner.equals("Denmark")) {
            //Go to Denmark activity
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
    }

});

As you can see, the application starts with the MainActivity, but since Denmark is the first String in strings.xml, the application goes directly to the "Denmark activity".

Add an empty string item on your string.xml, somthing like this:

<string-array name="country_names">
        <item></item>
        <item>Denmark</item>

    </string-array>

Update2

Or you can do this setSelection(position, false); in the initial selection before setOnItemSelectedListener(listener)

So update your code to this:

   spinner = (Spinner) findViewById(R.id.spinner);
    adapter = ArrayAdapter.createFromResource(this, R.array.country_names, android.R.layout.simple_spinner_item);

    adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);

    spinner.setAdapter(adapter);
    setSelection(0, false); 
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            ((TextView) parent.getChildAt(0)).setText(null);
            ((TextView) parent.getChildAt(0)).setTextColor(Color.BLACK);
            ((TextView) parent.getChildAt(0)).setTextSize(23);

            Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
            String txtFromSpinner = mySpinner.getSelectedItem().toString();

            if (txtFromSpinner.equals("Denmark")) {
                //Go to Denmark activity
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM