Spinner calling same function multiple times while opening Activity in android - android

ArrayAdapter<String> deptAdapter = new ArrayAdapter<>(Courses.this, R.layout.profile_spinner_textview, coursetype);
spinner1.setAdapter(deptAdapter);
Log.d("co_id", String.valueOf(coursetypeid));
ArrayAdapter<String> centAdapter = new ArrayAdapter<>(Courses.this, R.layout.profile_spinner_textview, centertype);
spinner2.setAdapter(centAdapter);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
co_id=coursetypeid.get(i);
Log.d("co_id",co_id);
loaddata();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
cent_id=centerid.get(i);
loaddata();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
while opening activity same function calling multiple times. how to avoid this. i want call same function while selecting items from spinners. please help me

Related

OnItemSelectedListener Spinner Recreate loop - Android

I have a spinner and when an item is selected i want to recreate the activity. But when the activity is recreated it keeps constantly recreating because the new itemSelectedListener is triggered. I have fixed the bug but i am interested in why this is happening. Thank you in advance for any insights you offer.
sp_lang.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
recreate();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
You can avoid the first event with a flag in the OnItemSelectedListener.
sp_lang.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
Boolean firstEventConsumed = false;
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (firstEventConsumed) {
recreate();
} else {
firstEventConsumed = true;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
EDIT
I have found a solution but it is just a workaround. It is not a definitive solution.
It has a strange behaviour when you recreate the activity. When the activity is created for the first time, it doesn't call twice the onItemSelected, but when it is recreated it is called twice.
What I have done below is to control when the item is selected by the user (handling the touch event) and when it is done by the activity.
public class MainActivity extends AppCompatActivity {
private Boolean isUserAction = false;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// get the spinner
// create the adapter
spinner.setAdapter(spinnerAdapter);
spinner.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
isUserAction = true;
return false;
}
});
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (isUserAction) {
recreate();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// do nothing
}
});
}
}

get spinner value and post to data base with volley

I'm going to POST value of spinners to the database
unfortunately, I do not know how to implement the POST method at any part of the CODE.
I'm having Problem with sending Snipper values.
public class FormActivity extends AppCompatActivity implements
....
spinnerStudy.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String all_study = spinnerStudy.getItemAtPosition(spinnerStudy.getSelectedItemPosition()).toString();
if(!Objects.equals(all_study, "")){
Toast.makeText(getApplicationContext(),all_study, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// DO Nothing here
}
});
spinnerCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String all_city = spinnerCity.getItemAtPosition(spinnerCity.getSelectedItemPosition()).toString();
if(!Objects.equals(all_city, "")) {
Toast.makeText(getApplicationContext(), all_city, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// DO Nothing here
}
});
s
pinnerUniversity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String all_university = spinnerUniversity.getItemAtPosition(spinnerUniversity.getSelectedItemPosition()).toString();
if(!Objects.equals(all_university, "")) {
Toast.makeText(getApplicationContext(), all_university, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// DO Nothing here
}
});
spinnerUnited.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String all_united = spinnerUnited.getItemAtPosition(spinnerUnited.getSelectedItemPosition()).toString();
if(!Objects.equals(all_united, "")) {
Toast.makeText(getApplicationContext(), all_united, Toast.LENGTH_SHORT).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// DO Nothing here
}
});
button.setOnClickListener(this);
...
PLEASE Help ME...In this CASE, the post method HOW sends parameters?
How to get From Spinner a public String? Thank you ALL
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//position is spinner position
//mySpinner.getSelectedItem().toString(); IS value of position (string)
mMap.put("your Key",mySpinner.getSelectedItem().toString());
//the value add to map of volley
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

Unable to get Spinner value when click item

I am here trying display seleted value from dropdown spinner, but mysetOnItemSelectedListener is not working
My code is as follows:-
ArrayAdapter<String> adapter =new ArrayAdapter<String>(this, R.array.location,list);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
Toast.makeText(
parent.getContext(),
"The color is "
+ parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this,R.array.location,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
Toast.makeText(
parent.getContext(),
"The color is "
+ spinner.getSelectedItem().toString(),
Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
Make necessary changes and try the above code.If it not works then change Charsequence to String.
try this code
spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Employee employee = employees.get(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

Android Spinner Item selected text

It looks pretty straight forward but it's not working for me. I am trying to get item text from a Spinner when Item is selected.
sp = (Spinner) findViewById(R.id.stnacspinner);
cspinner = aircraft.getDistinctStation();
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
Functions.CustomToastShort(getBaseContext(), sp.getSelectedItem().toString() );
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
It's returning "android.database.sqlite.Sq;LiteCursor#40e06dc0"
What am I doing wrong ?
well you are getting the string representation of the object which is memory address.
sp = (Spinner) findViewById(R.id.stnacspinner);
cspinner = aircraft.getDistinctStation();
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
Functions.CustomToastShort(getBaseContext(), sp.getSelectedItem().toString() );
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
sp.getSelectedItem().toString() //returns "android.database.sqlite.Sq;LiteCursor#40e06dc0"
selectedItemView.getText() or sp.getSelectedItem().getText() should do the trick.

Create Spinner Programmatically

I have to create a Spinner which is not in my Layout.
so I want to create it Programmatic I write the following code in My Activity class
Spinner spinner = new Spinner(this);
String option[]={"By Date","By Transaction ID","By Customer Phone"};
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,option);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item );
spinner.setAdapter(spinnerArrayAdapter);
spinner.performClick();
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentview, View v,int position, long id)
{
Log.d("in select","yes");
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
Toast.makeText(MainMenu.this,"no",Toast.LENGTH_LONG).show();
Log.d("In No select","No");
}
});
so My question is how I can access the spinner selected item in the activity
Your only way to access it outside your listener is making a variable that will hold the position, so you would use it later.
Something like this:
public class YourActivity extends Activity {
private int curPos;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Spinner spinner = new Spinner(this);
String option[]={"By Date","By Transaction ID","By Customer Phone"};
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,option);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item );
spinner.setAdapter(spinnerArrayAdapter);
spinner.performClick();
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentview, View v,int position, long id)
{
curPos = position;
Log.d("in select","yes");
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
Toast.makeText(MainMenu.this,"no",Toast.LENGTH_LONG).show();
Log.d("In No select","No");
}
});
}
In your onItemSelected:
#Override
public void onItemSelected(AdapterView<?> parentview, View v,int position, long id)
{
Log.d("in select","yes");
}
The position helps you to retrieve your selected item. Which, in your case, it should be:
option[position]

Categories

Resources