Can't get string from spinner - android

I have spinner that have 5 text string. I want to get a string from the spinner, but I only get the first string (i can't get the second, third.....).
i use this syntax(below) but still failed:
Spinner spinner = (Spinner) findViewById(R.id.spinnerItem);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.SpinnerArray, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
String SpinnerText = myspinner.getSelectedItem().toString();

By using onItemSelectedLIstener() method you can get each spinner value into string.
Main.java
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String selection=spinner.getSelectedItem().toString();
Toast.makeText(getApplicationContext(), "Selected" + selection, 30).show();
}

To get the selected item from the spinner first you need to set the listener for spinner using
spinner.setOnItemSelectedListener(this);
and u need to implement the interface OnItemSelectedListener
and finally override the methods
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
selection.setText(items[position]);
}
public void onNothingSelected(AdapterView<?> parent) {
selection.setText("");
}

try this code
in onclick listener of spinner use this code to get String of selected item
String s = spinneradapter.getItemAtPosition(Integer.parseInt(position));
hope this help

Use this :
String mySpinner = spinner.getItemAtPosition(spinner.getSelectedItemPosition()).toString();

spinner.getItemAtPosition(0).toString()//First string
spinner.getItemAtPosition(1).toString()//second string
spinner.getItemAtPosition(2).toString()//third string

see below code it may help you.
spin_search.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int id,
long arg3) {
Toast.makeText(Sms_logs.this, "you select : " + adapter.getItem(id), 2000).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});

Related

Display EditText items on Spinner selection

I have a group of items in spinner and on their click I want to show the respective text in EditText . How I can achieve this. I thought of using switch but it doesnot work for strings. I want someone to tell me right approach for doing this.
I want the EditText array(mysuburb) to respond accordingly to the Spinner items(mystate) click .
Code:-
String[] mysuburb =new String[]{"sub1" ,"sub2","sub3","sub4","sub5","sub6"};
String[] mystate= new String[]{"NSW","Victoria","Qld","NT","WA","SA"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), R.layout.listrow, mystate);
// LTRadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
state.setAdapter(adapter);
state.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,int pos, long arg3) {
// TODO Auto-generated method stub
sstate = state.getSelectedItem().toString();
/* String sub= state.getItemAtPosition(0).toString();
if(sub=="sub1")
suburb.setText("sub1") ; */
suburb.setText(arg0.getItemAtPosition(pos).toString());
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
state.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view,int i, long l) {
sstate = state.getSelectedItem().toString();
suburb.setText(sstate);
}
}

converting selected spinner value to defined int variable : android

I have an android program in which there is a spinner. i am getting the selected spinner value. i am able to send it as json too.
The problem is i want to send 1 if option1 is selected , 2 if option two is selected and so on.
Is there a method the selected value is converted to int value of my choice before i send it as json ?
the code is
public void addItemsOnSpinner1() {
s1 = (Spinner) findViewById(R.id.spinnerL);
List<String> list = new ArrayList<String>();
list.add("Casual");
list.add("Earned");
list.add("Compensatory");
list.add("Without Pay");
list.add("Sick Leave");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(dataAdapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
// Toast.makeText(getBaseContext(), s5.getSelectedItem().toString(),Toast.LENGTH_LONG).show();
final String SelectedLeave = s1.getSelectedItem().toString();
balanceLeavesLeft();
}
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
});
}
the 3rd param is the position of selected item of your spinner:
It's arg2 in your code
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
// Toast.makeText(getBaseContext(), s5.getSelectedItem().toString(),Toast.LENGTH_LONG).show();
final String SelectedLeave = s1.getSelectedItem().toString();
balanceLeavesLeft();
}
--> So you just need print out: (arg2+1)
tauitdnmd, answer is good but, the best way should be that you have an Entity class Opion as example and you have the string value for it and the int value of the selected row :) that's how is done usually.
public class YourOptionClass {
private String optionName;
private int optionValue;
//construktor
//getter and setter
//to string
}
And you populate your spinner with this class.
public void onItemSelected(AdapterView<?> arg0, View arg1,int pos, long arg3){
int option = YourAdapterClass.getItem(pos).getOptionValue();
}

Android - How to get the selected item value from a spinner and put it into a string?

i read many similar questions on this thread, but none of them help me...
This is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.imc_spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.imc_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
}
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// An item was selected. You can retrieve the selected item using
imc_met = parent.getItemAtPosition(pos).toString();
}
I declare imc_met as public String imc_met;. The problem is that imc_met does not contain the value of the selected item of the spinner, but it's null...
Where's the problem?
Thx in advance.
Use:
imc_met=Spinner.getSelectedItem().toString();
Instead:
imc_met = parent.getItemAtPosition(pos).toString();
Updated:
Seem you assigning Listener to your spinner not in correct way, do something like below:
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String imc_met=spin.getSelectedItem().toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Try this:
imc_met=Spinner.getSelectedItem().toString();
I'm sorry. I forgot
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
imc_met=Spinner.getSelectedItem().toString();
}
}
is
imc_met=spinner.getSelectedItem().toString();
not
imc_met=Spinner.getSelectedItem().toString();
int position = Arrays.asList(getResources().getStringArray(R.array.country_value_array)).indexOf(address.getCountry());
This will get index by the value.
If spinner isn't define in onCreate() method then have to use this:
String spinner_value = ((Spinner)findViewById(R.id.spinner1)).getSelectedItem().toString();
Try this code in your onCreate() method:
spinner.setOnItemSelectedListener(this);

How to get spinner control selected id

I've a spinner control that I connected it via a dataadapter and I get the data through it .
in the class, I want to get the selected spinner control's id .
How can I do that ?
I've used this code but when I run it , it says there's a problem and it closed .
Toast toast=Toast.makeText(MainActivity.this,sp.getSelectedItemId(),5000);
toast.setGravity(Gravity.CENTER,100, 0);
toast.show();
I tried getSelectedItemId and getSelectedItemPosition but non of them worked .
Here the answer for your question
ArrayAdapter<String> adpt = new ArrayAdapter<String>this,android.R.layout.simple_spinner_item, strType);
adpt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnType.setAdapter(adpt);
spnType.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3)
{
int intItemType = spnType.getSelectedItemPosition();
}
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
This the line getting selected position of the spinner
int intItemType = spnType.getSelectedItemPosition();
Try this
//Spinner OnItemClick Event here
payfeeTabStudentNameSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String spinnerSelectedValue = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Please let me know your problem is resolved or not.

Spinner on value Change

I have two spinner in my system. Now I have to change the selected value of the 2nd spinner depending on the first spinner value. As soon as the user will change the 1st spinner value, the 2nd spinner value will set automatically depending upon the 1st spinner's selected value. How to implement this?
From the Hello Spinner tutorial:
Now create a nested class that implements AdapterView.OnItemSelectedListener. This will provide a callback method that will notify your application when an item has been selected from the Spinner. Here's what this class should look like:
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext(), "The planet is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
The AdapterView.OnItemSelectedListener requires the onItemSelected() and onNothingSelected() callback methods. The former is called when an item from the AdapterView is selected, in which case, a short Toast message displays the selected text; and the latter is called when a selection disappears from the AdapterView, which doesn't happen in this case, so it's ignored.
Now the MyOnItemSelectedListener needs to be applied to the Spinner. Go back to the onCreate() method and add the following line to the end:
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
In other words, you need to create an OnItemSelectedListener that modifies the value of the second spinner, and attach it to the first spinner.
You have to put the condition on onItemSelected of very first spinner. By this example you can get value of 2nd spinner depending on 1st spinner:
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
if(arg0.equals(spin0)){
spin1.setClickable(true);
if(spin0.getSelectedItem().equals("India"))
{
ArrayAdapter <String> s1 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_india);
s1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin1.setAdapter(s1);
}
else if(spin0.getSelectedItem().equals("Pakistan"))
{
ArrayAdapter <String> s2 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_pak);
s2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin1.setAdapter(s2);
}
else if(spin0.getSelectedItem().equals("China"))
{
ArrayAdapter <String> s3 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_china);
s3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin1.setAdapter(s3);
}
}
}
You should define the onItemSelected() separately for each spinner. Otherwise the code gets executed if anything is selected from either spinners.
newCategory.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String selCat = newCategory.getItemAtPosition(arg2).toString();
if (selCat != "New")
{
loadSpinnerData(topic);
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
newTopic.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
loadSpinnerData()
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});

Categories

Resources