setOnItemSelectedListener in the Spinner not calling - android

I have such a method for items adding:
timezoneEdit.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
timezonSpin = (Spinner) findViewById(R.id.timezoneSpin);//timezoneCall();
final ArrayList <String> timezonelist = new ArrayList<String>();
timezonelist.add("Gvatemala");
timezonelist.add("Gonduras");
timezonelist.add("Batumi");
timezonelist.add("Boston");
timezonelist.add("Zhytomir");
timezonelist.add("Zmerinka");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(NewOrderActivity.this,
android.R.layout.simple_spinner_item, timezonelist);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
timezonSpin.setAdapter(dataAdapter);timezonSpin.performClick();
timezonSpin.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
Log.i("sd","Ya tut");
Log.i("choosed",timezonelist.get(position).toString());
timezoneEdit.setText(timezonelist.get(position));
}
public void onNothingSelected(AdapterView<?> arg0) {
TimeZone timezone = TimeZone.getDefault();
String TimeZoneName = timezone.getDisplayName();
int TimeZoneOffset = timezone.getRawOffset()/(60 * 60 * 1000);
Log.i("timezone",Integer.toString(TimeZoneOffset));
timezoneEdit.setText("UTC"+Integer.toString(TimeZoneOffset), TextView.BufferType.EDITABLE);
}
});
When user clicks on button, spinner is showing and user can choose one of the items. But weird thing is that onItemSelectedListener is not calling. I use such shema for my other regular spinners(without button click using) and everything works fine there...What's the missing?

timezoneEdit.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
Spinner timezonSpin = (Spinner) findViewById(R.id.timezoneSpin);//timezoneCall();
final ArrayList <String> timezonelist = new ArrayList<String>();
timezonelist.add("Gvatemala");
timezonelist.add("Gonduras");
timezonelist.add("Batumi");
timezonelist.add("Boston");
timezonelist.add("Zhytomir");
timezonelist.add("Zmerinka");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(NewOrderActivity.this,
android.R.layout.simple_spinner_item, timezonelist);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
timezonSpin.setAdapter(dataAdapter);timezonSpin.performClick();
timezonSpin.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
Log.i("sd","Ya tut");
Log.i("choosed",timezonelist.get(position).toString());
timezoneEdit.setText(timezonelist.get(position));
}
public void onNothingSelected(AdapterView<?> arg0) {
TimeZone timezone = TimeZone.getDefault();
String TimeZoneName = timezone.getDisplayName();
int TimeZoneOffset = timezone.getRawOffset()/(60 * 60 * 1000);
Log.i("timezone",Integer.toString(TimeZoneOffset));
timezoneEdit.setText("UTC"+Integer.toString(TimeZoneOffset), TextView.BufferType.EDITABLE);
}
});
try the above code. i think you have to make spinner object inside setOnClickListener event.

Take the timezonSpinner.setOnItemSelectedListener out the block timezoneEdit.setOnClickListener(new OnClickListener() { }

Related

To set text on spinner

I want to implement one code for three spinner where the user will fill there date of birth at register time when he/she will login and want to update there DOB, the DOB will pass on the spinner I have done it with day and month but there is one problem in year i.e adapter. adapter takes the (thisYear-18) year and print it but I want spinYear will take that value which I stored at registered time.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_details_fragment);
Spinner spinDay = (Spinner) findViewById(R.id.spinnerDay);
Spinner spinMonth = (Spinner) findViewById(R.id.spinnerMonth);
Spinner spinYear = (Spinner) findViewById(R.id.spinnerYear);
Button update = (Button)findViewById(R.id.update);
Intent intent =getIntent();
String str_DOB = intent.getStringExtra("dateofbirth");
Log.e("DOB************",str_DOB);
String[] d = str_DOB.split("-");
Log.e("d[0]-------",d[0].toString());
spinYear.setSelection (Integer.parseInt(d[0]));
spinMonth.setSelection (Integer.parseInt(d[1]));
spinDay.setSelection (Integer.parseInt(d[2]));
code for Year Adapter
ArrayList<String> years = new ArrayList<String>();
int thisYear = Calendar.getInstance().get(Calendar.YEAR);
thisYear=thisYear-18;
for (int i = thisYear; i >= 1967; i--) {
years.add(Integer.toString(i));
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, years);
spinYear.setAdapter(adapter);
spinYear.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {
final String yearName = adapterView.getItemAtPosition(pos).toString().trim();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
If I understand you correctly you want to set the selection of the year spinner based on your passed in Intent.
setSelection(int position) Takes the index of the item in the spinner you want to select.
After you call setAdapter do as so:
spinYear.setAdapter(adapter);
int index = years.indexOf(d[0]);
if (index != -1) {
spinYear.setSelection(index);
}
you should check the year in onItemSelected() method because this method get the position 0 and set it first.try to use below code:-
spinYear.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {
if((d[2]).equals(adapterView.getItemAtPosition(pos).toString()))
{
final String yearName = adapterView.getItemAtPosition(pos).toString().trim();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});

Open Text plain when spinner option get selected

i got a spinner with some options, and i want to change visibility of text box when the specific "other" option get selected , how should i do this ?
this.arraySpinner = new String[] {"Mobile", "Gaming", "Media Features", "Medical", "Sports And Nutrition","Security","Space","Other"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arraySpinner);
categories.setAdapter(adapter);
Try this:
.................
........................
this.arraySpinner = new String[] {"Mobile", "Gaming", "Media Features", "Medical", "Sports And Nutrition","Security","Space","Other"};
// Views
categories= (Spinner) findViewById(R.id.spinner);
textView = (TextView) findViewById(R.id.textView);
// Adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arraySpinner);
categories.setAdapter(adapter);
// Set onItemSelected listener to spinner
categories.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
TextView spinnerText = (TextView) view;
// Checking weather selected item is "Other" or not
if (spinnerText.getText().equals("Other"))
{
// Hide TextView
textView.setVisibility(View.INVISIBLE); // or View.GONE
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
.............
...................
Hope this will help
you can Implement OnItemSelected in the spinner and do the things what you want.
categories.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
if(arraySpinner!=null && arraySpinner[position].equalIgnoreCase("Other")){
// your code here
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});

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

setting itemListener of variable number of spinners

int numberofSpinner = TransportResult.Transfers.size();
Spinner spin=null;
for(int i=0;i<numberofSpinner;i++)
{
spin = new Spinner(this);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT,1);
spinLayout.addView(spin,p);
spin.setId(i);
Transfer transfer = TransportResult.Transfers.get(i);
ArrayList<CharSequence> s = new ArrayList<CharSequence>();
for( Line l : transfer.TransferLine)
{
s.add(l.ShortName+" - "+Helper.FindTransportTypeText(l.LineType));
}
adapter = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item,s);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter);
}
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if(parent.getId()==0){
System.out.println("spin 1 is called");
String str = (String)parent.getSelectedItem();
}else if(parent.getId()==1){
System.out.println("spin 2 is called");
String str = (String)parent.getSelectedItem();
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
If the number of spinner is more than 1, only the last spinner is triggered. For example; i have 3 spinner on screen, when i select the item of first or second spinner, the listener is never triggered. Only the third spinner triggers the listener. How can i solve that?
Thank you
UPDATE
when you use more than one spinner then setid for each spinner spin.setId(int) .and you can check id in OnItemSelected method. Mind that when you set OnitemSelected first time onItemSelected is called.
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if(parent.getId()==1){
System.out.println("spin 1 is called");
String str = (String)parent.getSelectedItem();
}else if(parent.getId()==2){
System.out.println("spin 2 is called");
String str = (String)parent.getSelectedItem();
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});

Adding OnItemSelectedListener to Spinner

I have a button and a spinner (originally hidden). When user presses a button, spinner gets populated with items and becomes visible. Now I would like to add OnItemSelectedListener to the spinner. and I have tried many tutorials with no luck.
This is my OnCreate function
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button load_routes = (Button)findViewById(R.id.load_routes);
Spinner routes = (Spinner)findViewById(R.id.routes_list);
load_routes.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
load_routes(v);
}
});
routes.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0, View v, int position, long id)
{
Log.v("routes", "route selected");
}
public void onNothingSelected(AdapterView<?> arg0)
{
Log.v("routes", "nothing selected");
}
});
}
This is my load_routes function
private void load_routes(View v)
{
Spinner routes = (Spinner)findViewById(R.id.routes_list);
List<String> routes_list = RouteParser.get_routes();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, routes_list);
routes.setAdapter(adapter);
TableRow list_of_routes_row = (TableRow)findViewById(R.id.list_of_routes_row);
list_of_routes_row.setVisibility(View.VISIBLE);
}
This set up does not work. The only way I got this to work is when I setup my listener as routes.setOnItemSelectedListener(this) Then I implement OnItemSelectedListener and include the functions neccessary. But I have multiple spinners and need to create separate listeners for different spinner. Any help will be appreciated. Thanks!
final String[] s2 = getResources().getStringArray(R.array.capteur_size);
final EditText ed = (EditText) findViewById(R.id.editTextCoC);
spinnerCoC = (Spinner) findViewById(R.id.spinnerCoC);
spinnerCoC.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
ed.setText(s2[arg2]);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Declare your Spinner as field instantiate the listener once you do findViewById and use it wherever you want.

Categories

Resources