Spinner Scroll Selected Value - android

RecyclerView not saving Spinner values.
I have tried available examples all on stackoverflow, I could not find a working solution.
Please find my code of my RecyclerView Below:
ArrayAdapter<CharSequence> countryAdapter = ArrayAdapter.createFromResource(context, R.array.spinnerlist, android.R.layout.simple_spinner_item);
countryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
valueselected.setAdapter(countryAdapter);
valueselected.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
valueselected.setSelection(i, true);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
valueselected.setAdapter(countryAdapter);
valueselected.getSelectedItemPosition();
Once scrolled, the selected value of the spinner needs to remain.

Related

How do I know the position of selected item in spinner?

I am inflating Spinner along with that I am also inflating some other views
LinearLayout child_linear = (LinearLayout) findViewById(R.id.child_linear);
LayoutInflater inflater = getLayoutInflater();
View myView = inflater.inflate(R.layout.add_custome_layout, child_linear, false);
name = (TextView) myView.findViewById(R.id.name);
price = (TextView) myView.findViewById(R.id.add_price);
names_spinner = (Spinner) myView.findViewById(R.id.product_addmore_spinner);
child_linear.addView(myView);
Now I getting data for the spinner. The question if there are 'n' no.of spinners how do I know which spinner item user selected?
names_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
parent.getItemAtPosition(position).toString();
if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("Select Toppings")) {
//Show PopUp or Alerts
Toast.makeText(ProductView.this, "position" + names_spinner.getItemIdAtPosition
(position), Toast.LENGTH_SHORT).show(); }
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Even when I click on particular spinner item position is also not showing in Toast. What's my mistake?
You are showing toast only when user click on Select Toppings. Take a look to your code:
if (parent.getItemAtPosition(position).toString().equalsIgnoreCase("Select Toppings")) {
Change this line for
if (!parent.getItemAtPosition(position).toString().equalsIgnoreCase("Select Toppings")) {
And alerts should be shown.
Additionally to know which item is clicked, the listener provided you the clicked position, you can search in you items array this position and you can know which item is.
EDIT:
Now, lets suppose you have more than one spinner. spinner1 and spinner2, for example. You could register an OnItemSelectedListener for each one.
Then you can create a global variable called Spinner clickedSpinner and inside the OnItemSelectedListener of the spinner1 you set the clickedSpinner to spinner1 and inside the OnItemSelectedListener of the spinner2 set the clickedSpinner to spinner2:
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
clickedSpinner = spinner1;
... // Do your stuff here
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
And:
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
clickedSpinner = spinner2;
... // Do your stuff here
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
So, after clicking any item of any Spinner you can now know which Spinner was clicked because is stored in clickedSpinner.
Please note that this variable is not mandatory, you could achieve the same behavior without this global variable, because in each OnItemSelectedListener you know which spinner is the clicked. So, you could do the following:
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int spinner1Position = spinner1.getItemIdAtPosition(position);
... // Do your stuff here
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
And:
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int spinner2Position = spinner2.getItemIdAtPosition(position);
... // Do your stuff here
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Without using a global variable.
Hope this helps.
Try this
final Spinner names_spinner = (Spinner) myView.findViewById(R.id.product_addmore_spinner);
ArrayAdapter<String> madapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.yourarray));
madapter.setDropDownViewResource(android.R.layout.simple_list_item_1);
names_spinner.setAdapter(madapter);
names_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String mySpinner = (String) adapterView.getItemAtPosition(i);
Log.i("names_spinner",mySpinner);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
Toast.makeText(getApplicationContext(),"Your Message",Toast.LENGTH_LONG).show();
}
});
}

Cannot get current spinner position with setOnItemSelectedListener

I am trying to retrieve the spinner's selected position. The values inside the spinner are of type String and I am using setOnItemSelectedListener to change a class String variable to the one that has been selected and use it later on:
spinner = (MaterialBetterSpinner) myToolbar.findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,socialMediaOptions);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
socialMedia = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
private void sendPost(){
Log.v(TAG, socialMedia);
}
The problem is that the variable does not change when an item is selected, it seems like onItemSelected is not being called when there is a change in the spinner selection
See this thread:
https://github.com/Lesilva/BetterSpinner/issues/42
There said that BetterSpinner extends EditText, that's why onItemSelectedListener is not being called. You should use TextChangedListener instead.
Hope your socialMediaOptions are string array like this
socialMediaOptions={"FB","TWIT","IN"}
then
// Specify the layout to use when the list of choices appears
add this line
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
socialMedia = socialMediaOptions[position];
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

spinner with ItemSelected

i have spinner with array that i create in a string.xml which called array.spinner_title
how can i do the on item select her so i can get the item selected
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( SendTeacher.this,
R.array.spinner_title, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
// Do something
}
#Override
public void onNothingSelected(AdapterView<?> adapter) {
}
});
thanks
As I understand your question , you want to get the selected item.
In:
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
CharSequence item = (CharSequence)adapterView.getAdapter().getItem(position);
}
For custom adapter which can contain any other objects (not only CharSequence), see this link for example

android:opening new intent in spinner view

I am making an app in which i have to use spinner view to show some items and i want on click of item it should go to that page. i want when i clicke on spanish i should go to spanish pageMy code is as follows.
System.out.println("test1");
Spinner spinner = (Spinner) findViewById(R.id.spinner);
System.out.println("test2");
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.models, android.R.layout.simple_spinner_item);
System.out.println("test3");
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
System.out.println("test3");
spinner.setAdapter(adapter);
System.out.println("test4");
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
You have to write intent in your onItemSelected method for page/activity which you want to open.
and set position for that.
#Override
public void onItemSelected(AdapterView<?> adaptername, View view,int position, long id)
{
if(position==0)
{
// write the intent for page which you want to open
}
if(position==1)
{
//
}
.
.
.
and same
}

Tips for relating spinners

I have a need to add 3 related spinners where the selected content in the first affects what is displayed in the second and then what is selected in the second affects the content in the third. Has anyone already faced this problem and how did you over come it?
I was thinking of maintaining a number of arrays in the resources which are numbered and then loaded into the adapter based on the choice selected in the previous spinner. Is this a good way?
Thanks,
m
I would have done this with something like that:
spinner1.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
SpinnerAdapter adapter = ...create or load the second adapter based on selected item...
spinner2.setAdapter (adapter);
spinner3.setAdapter (..create empty adapter...);
}
#Override
public void onNothingSelected(AdapterView<?> parentView)
{
spinner2.setAdapter (..create empty adapter...);
spinner3.setAdapter (..create empty adapter...);
}
});
spinner2.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
SpinnerAdapter adapter = ...create or load the third adapter based on selected item...
spinner3.setAdapter (adapter);
}
#Override
public void onNothingSelected(AdapterView<?> parentView)
{
spinner3.setAdapter (..create empty adapter...);
}
});

Categories

Resources