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();
}
Related
I am trying to get the spinner to display the item which i have selected. But it is only displaying the first word even if i choose the ones below. Here is the code i am using
ArrayAdapter<String> aa1 = new ArrayAdapter<String>(
getApplicationContext(), R.layout.spinner_item, R.id.textView1, al);
spFacilityType.setAdapter(aa1);
spFacilityType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
int index = arg0.getSelectedItemPosition();
position = index;
}
});
final String Strspinner = spFacility.getItemAtPosition(position).toString();
Use onItemSelected instead of onNothingSelected.
do the code in onItemSelected()..
String s= spFacilityType.getSelectedItem().toString();
now s will show the selected item
I want to save my spinner state even when I move to a different activity and come back to the page with the spinner, the state should be the same. I have no idea how to do it, I saw some examples on other threads on stack overflow but I don't understand them.
Here's my code:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
index = arg0.getSelectedItemPosition();
// storing string resources into Array
lang_list = getResources().getStringArray(R.array.language_list);
Toast.makeText(getBaseContext(), "You have selected : " + lang_list[index],
Toast.LENGTH_SHORT).show();
choice = spinner.getSelectedItem().toString();
// edit_cate.setText(choice);
final ImageView country_flag = (ImageView)findViewById(R.id.country);
String s=((TextView)arg1).getText().toString();
if(s.equals("English"))
country_flag.setImageDrawable(getResources().getDrawable(R.drawable.eng_spinner));
if(s.equals("German"))
country_flag.setImageDrawable(getResources().getDrawable(R.drawable.german_spinner));
if(s.equals("French"))
country_flag.setImageDrawable(getResources().getDrawable(R.drawable.french_spinner));
if(s.equals("Spanish"))
country_flag.setImageDrawable(getResources().getDrawable(R.drawable.spanish_spinner));
}
Thanks for the help.
You need to save state of your spinner so this would be helpful to you.
1.) Apply this after creating spinner object
spinner.setSelection(getPersistedItem());
2.) Create these methods according to you to save the state of your spinner selected item
private int getPersistedItem() {
String keyName = makePersistedItemKeyName();
return PreferenceManager.getDefaultSharedPreferences(this).getInt(keyName, 0);
}
protected void setPersistedItem(int position) {
String keyName = makePersistedItemKeyName();
PreferenceManager.getDefaultSharedPreferences(this).edit().putInt(keyName, position).commit();
}
private String makePersistedItemKeyName() {
return currentUserName + "_your_key";
}
3.) Set its state as the spinner selection changed:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View view, int position, long itemId) {
setPersistedItem(position);
} #Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
} });
I would like to ask a very general question.
" How to filter based on spinner?"
Meaning, there's few option in spinner ("education", "musuem", "restaurant")
Upon selecting "museum" it will show me a list of musuem.
Is there such a way to do it?
I've those data retrieved, just that, I would like to know whether spinner can do this function.
I've google it but doesn't seems to find the answer that I wanted
thus, would like to seek for advice whether is there any such sources.
CHANGES IN QUESTION, SOMEHOW SIMILAR
If I've this coding, and wanted to retrieve either "Museum", "Singapore", or "Centre",
How should I edit in my code?
Meaning, upon click on the selection in spinner,
it will change.
MainActivity.java
public class MainActivity extends ListActivity {
String[] Category = {
"Singapore discovery Centre",
"Singapore Science Centre",
"Mint Museum",
"Singapore Art Museum",
"Army Museum"
};
String [] keywords = {
"Centre",
"Musuem",
"Singapore",
};
Spinner s1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//GridView
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,Category));
//SpinnerView
s1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, keywords);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0,View arg1, int arg2, long arg3) {
int index = s1.getSelectedItemPosition();
Toast.makeText(getBaseContext(), "You have seleted item :" + keywords[index] , Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?>arg0) {}
});
}
public void onListItemClick(ListView parent, View v, int position,long id)
{
Toast.makeText(this, "You have selected " + Category[position], Toast.LENGTH_SHORT).show();
}
}
first of all you should implement OnItemSelectedListener.
Spinner sp;
String[] strarr={"education", "musuem", "restaurant"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(filename.this,android.R.layout.simple_spinner_item,strarr);
sp=(Spinner) findViewById(R.id.spinner1);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(this);
you should override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
int position =sp.getSelectedItemPosition();
//things to do
}
and
public void onNothingSelected(AdapterView<?> arg0) {
//things to do
}
Yes there is a Listener named OnItemSelectedListener which tells you which item selected in the spinner so you can find the selected item id, then filter your adapter or whatever data structure you have with that selected Item.
Spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
})
});
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.
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
}
});