Item is not getting selected on spinner when used in recyclerview - android

I have a weird problem when using spinner widget inside a recyclerview. So the story is like this I have a recyclerview which has the spinner and I am passing list which needs to be inflated from fragment to the recyclereview where I am creating adapter if the data is not null and setting it into the spinner, but it works sometimes and most of the times not. It also works when getting focus or typing in edit text below it.
Note: I have already checked the other questions related this and yes I am using a white background with black text color layout which is basically a custom layout for the spinner. I am having a hard time figuring out what is going on.
This is my code for setting spinner in onBindViewHolder()
#Override
public void onBindViewHolder(FamilyMemberAdapter.ViewHolder holder, int position) {
FamilyMemberRecyclerViewModel familyMemberModel = familyMemberRecyclerViewModelList.get(holder.getAdapterPosition());
if (familyMemberModel.getRelationTypeModelList()!=null){
relationTypeModelArrayAdapter = new ArrayAdapter<RelationTypeModel>(context,R.layout.item_spinner,familyMemberModel.getRelationTypeModelList());
holder.spnRelationType.setAdapter(relationTypeModelArrayAdapter);
if (familyMemberModel.getRelationTypeAdapterModel()!=null){
int positionRelation = relationTypeModelArrayAdapter.getPosition(familyMemberModel.getRelationTypeAdapterModel());
holder.spnRelationType.setSelection(positionRelation);
}
}
}
spinner onItemSelectedListener()
spnRelationType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//here differentiate between different doc and relation
uploadDocumentItemsClickListener.onSpinnerChangeListener(spnRelationType,getAdapterPosition(),adapterView.getSelectedItemPosition());
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Here is snapshot of what is going on.
This is I am selecting item from the spinner
This is after selecting an item from the spinner.
Please let me know if more code needed.

I believe this is because the edit text still has the focus and not on the spinner. Try something like this.
spinner.setFocusableInTouchMode(true);
spinner.setOnFocusChangeListener((v, hasFocus) -> {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
if (spinner.getWindowToken() != null) {
spinner.performClick();
}
}
}
});

Related

How to setOnItemSelectedListener on several Dynamically created spinners

I have an Activity where I need to create 1 or more spinners dynamically according to an external DB.
SOme of this spinner items have to show a dialog according what value does the spinner has. For example the spinner has this options:
-Own
-Rental
-Family House
If the user selects Rental I have to show a dialog (or anything) asking him how much does he pays per month. If he selects own, or family nothing should happen.
After I create the layout with the spinners, edittexts, etc. Im using something like this:
for(int q=0;q<=parent.getChildCount();q++){
View v = parent.getChildAt(q);
if (v instanceof Spinner) {
Spinner res = (Spinner) v;
res.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//Here its supposed to show dialog if the option is "RENT"
}
public void onNothingSelected(AdapterView<?> adapterView) {
return;
}
});
}
}
The problem is that when I do this the "setOnItemSelectedListener" only sets for the last spinner on the layout.
How can I do what Im trying? I dont know what else to do.
The easiest solution would probably be to make one Listener as a variable and use that for all of your spinners. To do this, you would not set it as you are currently (using the anonymous inner-class style) and instead would do this:
//This goes outside of the method
private AdapterView.OnItemSelectedListener listener =
new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
System.out.println("Spinner Selected ID = " + parent.getId());
/*
Put a check here for which one is being selected.
While you could use the parent to check, in your case, it will be easier
to use something from your DB table as a unique identifier (maybe a column
name would be ideal? Your pick)
*/
//Show your dialogs here
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
return;
}
};
//This is the method you have where you are iterating the parent object
private void doStuff(){
for(int q=0;q<=parent.getChildCount();q++){
View v = parent.getChildAt(q);
if (v instanceof Spinner) {
Spinner res = (Spinner) v;
res.setOnItemSelectedListener(listener);
}
}
}
Good luck to ya!

Set Spinner Adapter without calling onItemSelected

My codes goes like this.
spinner.setAdapter(mAdapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
chosenPosition = position;
spinner.setEnabled(false);
}
My intention is to save the position chosen by user and store it and immediately disable the spinner. But, when I set the Adapter, the first item automatically gets selected and the spinner gets disabled. How do I prevent it from calling the onItemSelected method when the Adapter is set?
Thank You.
Define a flag on top such as :
boolean isFirstSelection = true;
and in your onItemSelected method:
spinner.setEnabled(isFirstSelection);
if(isFirstSelection) {
isFirstSelection = !isFirstSelection;
}

Android Spinner not calling onItemSelected

EDIT: I did a workaround and it SORT OF fixed my issue.
My layout for the spinner looks like
<LinearLayout>
<Spinner>
<TextView> //used as a label
</LinearLayout>
I removed the LinearLayout. Though the City spinner does not become auto-populated with items whenever a State is selected, I am able to manually input values for the City.
What could the linearlayout possibly do to mess up my spinner's behavior?
I have a registration form with multiple Edittexts and 3 spinners at the bottom namely State, City, Area.
What I want to be able to do is the following
onItemSelect a State, City and Area will show the first items in their list
onItemSelect a City, Area will show the first item in its list
onItemSelect an Area, it will show the item I selected.
Code is working fine on Kitkat. But on JellyBean and below, whenever I select a State, the other spinners do not show anything. After selecting a State, when you click on the City, it will show correct dataset, however when I click on an item the spinner remains blank. (onItemSelected does not fire.)
The weird part is that, whenever the spinners misbehave, if I click on one of the Edittexts in my view (softKeyboard will be shown) > Press back button > Spinner works. I'm not sure if it has something to do with focus? But I already tried to set the spinners to focusable and focusableInTouchMode then request focus, but to no avail.
this is how I set the adapters and content of the spinners
state.setAdapter(stateAdapter);
state.setSelection(0, false);
state.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
stateSelection = parent.getItemAtPosition(position).toString();
cityAdapter = new ArrayAdapter<String>(getActivity(), R.layout.sherlock_spinner_item, getDistinctCity(stateSelection));
cityAdapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
city.setAdapter(cityAdapter);
areaAdapter = new ArrayAdapter<String>(getActivity(), R.layout.sherlock_spinner_item, new String[]{});
areaAdapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
area.setAdapter(areaAdapter);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
city.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
citySelection = parent.getItemAtPosition(position).toString();
areaAdapter = new ArrayAdapter<String>(getActivity(), R.layout.sherlock_spinner_item, getDistinctArea(stateSelection, citySelection));
areaAdapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
area.setAdapter(areaAdapter);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
area.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
areaSelection = parent.getItemAtPosition(position).toString();
postalSelection = getDistinctPostal(stateSelection, citySelection, areaSelection);
postal.setText(postalSelection);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
If anyone can explain to me why the spinners are behaving like this, or possibly lead me to the right direction, that would be awesome. Thank you

Android Spinner taking time, after select value from adapter

In my application there are lot's of views(spinner, buttons, editText, textView, ImageButton etc).
After selecting one value in the spinner it not reflect the value which I selected. The old value is till there on the spinner. But when I tap on other views like editTest then spinner value automatically updated.
I think no need to mention the code because normal spinner is there which populating data from ArrayAdapter and I am doing small task after selecting value.
CODE:-
spinnerWard = (Spinner) findViewById(R.id.spinnerWard);
aAdapterWard = new ArrayAdapter<WardList>(this,
android.R.layout.simple_spinner_item, listWard);
aAdapterWard.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerWard.setAdapter(aAdapterWard);
spinnerWard.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v,
int pos, long id) {
intWardPosition = pos;
intFinalWardId = listWard.get(pos).getId();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {}
});

Change The Adapter when Click On Spinner

I need to change the Adapter of my Spinner exactly at the moment when I click on it.
I'm trying to display a Spinner with The value "Make Your choice" and then when the user click, another adapter is loaded on the Spinner and he can make his choice (without displaying the "Make Your choice").
Here is my code
ArayAdapter adapterclasse = new ArrayAdapter(this,android.R.layout.simple_spinner_item, affp.classes);
ArrayAdapter adaptermodule = new ArrayAdapter(this,android.R.layout.simple_spinner_item, affp.matiers);
spinner.setAdapter(adapterclasse);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
spinner.setAdapter(adaptermodule);
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
but it's not working. When the app is launched, I got the second adapter loaded instead of the first.
Please help and thanks.
The second adapter is set because when the spinner is loaded the method onItemSelected is called, that's normal (the first element is selected).
Anyway, in general I don't think you need 2 adapters, you can just put "Make Your choise" on top and onItemSelected if the user has selected that, you can stop him and ask to select a real value.
Actually, it's possible to load a new adapter when the user touch the view (spinner)
spinner.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
spinner.setAdapter(adapterclasse2);
return false;
}
});

Categories

Resources