Android Spinner:Avoid Onitemselected Calls During Initialization

Android Spinner : Avoid onItemSelected calls during initialization


spinner.setOnItemSelectedListener(this); // Will call onItemSelected() Listener.

So first time handle this with any Integer value

Example:
Initially Take int check = 0;

public void onItemSelected(AdapterView<?> parent, View arg1, int pos,long id) {
if(++check > 1) {
TextView textView = (TextView) findViewById(R.id.textView1);
String str = (String) parent.getItemAtPosition(pos);
textView.setText(str);
}
}

You can do it with boolean value and also by checking current and previous positions. See here

How to keep onItemSelected from firing off on a newly instantiated Spinner?

I would have expected your solution to work -- I though the selection event would not fire if you set the adapter before setting up the listener.

That being said, a simple boolean flag would allow you to detect the rogue first selection event and ignore it.

Spinner onItemSelected called erroneously (without user action)

Androider, I have found a solution for this problem and posted it here (with code sample):

Spinner onItemSelected() executes when it is not suppose to

Spinner onItemSelected nedded only once, Android Studio

Use static variable to hold the last selection and check that before recreating the activity which give you flavor like onChangeListener

//Place inside Harta like global variable
static String lastCountryName = "";

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TaraITEM clickedItem = (TaraITEM) parent.getItemAtPosition(position);
String clickedCountryName = clickedItem.getCountryName();

// compare here before recreating
if(!clickedCountryName.equals(lastCountryName)) {
lastCountryName = clickedCountryName;

if (clickedCountryName.equals("FR")){
setLocale("fr");
recreate();
} else if (clickedCountryName.equals("EN")){
setLocale("en");
recreate();
}
}
}

Undesired onItemSelected calls

I found a simple and, I think, elegant solution.
Using tags.
I first created a new XML file called 'tags' and put in the following code:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
<item name="pos" type="id" />
</resources>

Whenever I myself use spin.setSelection(pos), I also do spin.setTag(R.id.pos, pos), so I am setting the current position as a tag.

Then, in onItemSelected, I am executing code only if(spin.getTag(R.id.pos) != position), where position is the position variable supplied by the function.
In this way, my code is executed only when the user is making a selection.
Since the user has made a selection, the tag has not been updated, so after the processing is done, I update the tag as spin.setTag(R.id.pos, position).

NOTE: It is important to use the same adapter throughout, or the "position" variable might point to different elements.

EDIT: As kaciula pointed out, if you're not using multiple tags, you can use the simpler version, that is spin.setTag(pos) and spin.getTag() WITHOUT the need for an XML file.

Spinner OnItemSelected event not being called

Problem was an Async Race Condition. Fixed by moving the Spinner setup into the processFinished method in DeptTask.

DeptTask deptTask = new DeptTask(new DeptResponse() {
@Override
public void processFinished(List<Department> output) {
Log.i("Department", "Finished");

ArrayAdapter<Department> deptAdapter = new ArrayAdapter<Department>(
getApplicationContext(),
R.layout.spinner,
deparments
);


deptAdapter.setDropDownViewResource(
R.layout.spinner
);


spinner.setAdapter(deptAdapter);

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Log.i("Selected Item", "I AM INDEED WORKING");
filter((Department)spinner.getSelectedItem());

}



@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}
});
}


});

Problem with Spinner setOnItemSelectedListener

To avoid calling spinner.setOnItemSelectedListener() during initialization

spinner.setSelection(Adapter.NO_SELECTION, true); //Add this line before setting listener
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});


Related Topics



Leave a reply



Submit