Android -How do i make the Spinner to show the item after selecting the Items from the Spinner?

亡梦爱人 提交于 2019-12-11 20:04:26

问题


This is my code:

    package com.testotspeech;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Locale;

import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class AndroidTestToSpeechActivity extends Activity implements
        TextToSpeech.OnInitListener, OnItemSelectedListener {
    /** Called when the activity is first created. */

    private TextToSpeech tts;
    private Button btnSpeak;
    private EditText txtText;
    private ArrayList<String> itemsList;
    private Spinner spinner;
    private String contry_name;
    private ArrayAdapter<String> dataAdapter;
    private TextView textview;
    private Iterator itr;
    private String[] t = {"Please Select An Item"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.i("----------",Arrays.toString(Locale.getAvailableLocales()));
        itemsList = new ArrayList<String>();
        itemsList.add(Arrays.toString(Locale.getAvailableLocales()));
        spinner = (Spinner)findViewById(R.id.spinner1);
        spinner.setOnItemSelectedListener(this);


        dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,t);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
        spinner.setAdapter(dataAdapter);
        textview = (TextView)findViewById(R.id.textView1);






        tts = new TextToSpeech(this, this);
        btnSpeak = (Button) findViewById(R.id.btnSpeak);
        txtText = (EditText) findViewById(R.id.txtText);

        // button on click event
        btnSpeak.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                speakOut();
            }

        });
    }

    @Override
    public void onDestroy() {
        // Don't forget to shutdown tts!
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }

    public void onInit(int status) {

        if (status == TextToSpeech.SUCCESS) {

            int result = tts.setLanguage(Locale.ENGLISH);

            if (result == TextToSpeech.LANG_MISSING_DATA
                    || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Log.e("TTS", "This Language is not supported");
            } else {
                btnSpeak.setEnabled(true);
                speakOut();

            }

        } else {
            Log.e("TTS", "Initilization Failed!");
        }

    }

    private void speakOut() {

        String text = txtText.getText().toString();
        tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
    }

    public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {

        if (position == 0)
        {
        }
        else
        {

        }
    }

    public void onNothingSelected(AdapterView<?> parent) {
        textview.setText("");
    }


}

EDITED:

What i want to do is when i click on thw spinner it will collapse/open down and i will have for each item and box of it self and it will be in row for example i clicked on the spinner i will see now under it:

Hello

Bye

Daniel

and now if i click on Hello it will put the Hello in the textView1 if i click on the Bye it will put it also in textView1 and so on. But the graphics the designing of the spinner i want it to be that when i click on it it will collapse down and show me the items in a row so i can click and select each item in a single click.

Now what i did is just adding the spinner a text "Please Select An Item" I uploaded image of how i wanted it to be like for example:


回答1:


That is the behavior of the spinner, there is always a selected item.

You can create a flag and use that in an override of the onItemSelected, put a counter in there and use that to ignore the first time in.

Like this:

private int spinnerSelectCount = 0;

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    if(spinnerSelectCount == 0) {
        // Do nothing... initial item on spinner display is the selected item
    } else {
        // your code to process spinner selection here
    }
    }
});

EDIT

You need to put your string as the first item in your array, not a separate string.

itemsList = new ArrayList<String>();  
itemsList.add("Please Select An Item");
itemsList.add(Arrays.toString(Locale.getAvailableLocales())); 

Then call the spinner using the array:

    dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,itemsList);        
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);        
    spinner.setAdapter(dataAdapter);  

And since you're doing that you should change the filter to be based on position, not count.

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    if(pos == 0) {
        // Do nothing... initial item on spinner display is the selected item
    } else {
        // your code to process spinner selection here
        textview.setText(itemsList.get(pos));
    }
    }
});

EDIT 2

Are you looking for multi-selection? That's not possible with a standard spinner, it is set up to select one item only. If you are really looking to select multiple items you need to use a list or create a custom spinner.

Fortunately, it looks like someone has already done that. You can find the code in this SO Answer

EDIT 3

The MultiSpinner does not need a new project. You merely create a new class and use that to populate your spinner.

1) Create a new class called MultiSpinner
2) Copy the code from link (leave out the package name as you'll want to use your own)
3) In your xml call out the spinner like so (replacing com.yourpackage.name with your actual package name):

<com.yourpackage.name.MultiSpinner android:id="@+id/multi_spinner" /> 

4) Call the spinner as shown in the link:

MultiSpinner multiSpinner = (MultiSpinner) findViewById(R.id.multi_spinner); 
multiSpinner.setItems(items, getString(R.string.for_all), 
                this); 



回答2:


An easy solution would be to set the first item of the spinner to a text like "Please select a value..". Then in the code..

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    if(pos == 0) {
    // do nothing..
    } else {
    // your code..
    }
}
});


来源:https://stackoverflow.com/questions/10925868/android-how-do-i-make-the-spinner-to-show-the-item-after-selecting-the-items-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!