Spinner only saving first word to string - android

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

Related

Is there a way to display the drop down spinner list on page loads in android?

I want to display the list of my array in the spinner when the page first loads and also when the user touches on the spinner.
I am doing this :
String[] simpleArray = {"java","android","Data Structures","HTML","CSS"};
ArrayAdapter<String> usageListAdapter = new ArrayAdapter<String>(this, R.layout.spinner_small_text, simpleArray);
usageListAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
btn_list_type.setAdapter(usageListAdapter);
btn_list_type.setBackgroundColor(0x00000000);
btn_list_type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long id) {
listTypePickerSelected(pos);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});

Android Spinner setSelection calls onItemSelected immidiately or not?

I have 2 spinners in an activity.
Based on the selection of one item in spinner1, relevant data should be loaded in spinner2. Consider spinner1 has data related to country and spinner2 has data related to state.
I should be able to get this done once the activity is created and if the user changes the selection in spinner1.
But I am stuck with populating the spinner2 data based on the saved value of spinner1.
I am calling spinner1.setSelection(indexSaved) but since I am only loading spinner2 in the onItemSelectedOf of spinner1, setSelection of spinner1 is not firing the onItemSelected.
Please let me know how I can achieve this.
I have done same thing in my project without any problem:
Below is code may be it help you:
On Create Method filled state list:
//initialize spinners
spinnerStateList = (Spinner) findViewById(R.id.spinnerStateList);
spinnerDistrictsList = (Spinner) findViewById(R.id.spinnerDistrictsList);
fillStateList();
public void fillStateList()
{
states = manage_states.fetchAllStates(getApplicationContext());
//add new element for select name
states.add(0,new RowItem("-1", "Select State"));
String[] spinnerArray = new String[states.size()];
for(int i= 0;i<states.size();i++)
{
spinnerArray[i] = states.get(i).getName();
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,spinnerArray);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerStateList.setAdapter(dataAdapter);
spinnerStateList.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
String selectedStateID = states.get(position).getId();
fillDistrictsSelectionChangeState(selectedStateID);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
public void fillDistrictsSelectionChangeState(String selectedStateID)
{
if(selectedStateID.equalsIgnoreCase("-1"))
{
linearLayoutDistrictsList.setVisibility(LinearLayout.GONE);
linearLayoutBlocksList.setVisibility(LinearLayout.GONE);
linearLayoutPHCList.setVisibility(LinearLayout.GONE);
linearLayoutSHCList.setVisibility(LinearLayout.GONE);
}
else
{
linearLayoutDistrictsList.setVisibility(LinearLayout.VISIBLE);
districts = manage_districts.fetchAllDistrictsByStateId(getApplicationContext(), selectedStateID);
//add new element for select name
districts.add(0,new RowItem("-1", "Select District"));
String[] spinnerArray = new String[districts.size()];
for(int i= 0;i<districts.size();i++)
{
spinnerArray[i] = districts.get(i).getName();
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,spinnerArray);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerDistrictsList.setAdapter(dataAdapter);
spinnerDistrictsList.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
String selectedDistrictID = districts.get(position).getId();
fillBlocksSelectionChangeDistrict(selectedDistrictID);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
}

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();
}

Does android have this function "How to filter based on spinner?"?

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) {
})
});

Can't get string from spinner

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
}
});

Categories

Resources