I am using spinners which are dependent on one another. So when i choose one value in a spinner A the arraylist of other spinners B,C,D change accordingly.
when I select item at position 1 on spinner B and then choose any other value in spinner A The data on spinner B gets updated. So I again choose item at position 1 on spinner B the view doesn't changes while selecting item at any other position changes the view. So the same index doesn't get updated value in this piece of code.
ArrayAdapter<String> myAdapterEmployer = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1,employerlist);
myAdapterEmployer.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
employer.setAdapter(myAdapterEmployer);
employer.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id)
{
if(position > 0) {
employer1 = employer.getSelectedItem().toString();
employeridname = employdetail.get(employer1);
Sectorlist(employeridname);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
This changes data of Sectorlist everytime a item is selected in employer.
And then any item is choosen in sectorlist.
ArrayAdapter<String> myAdaptersector = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1,sectorlist);
myAdaptersector.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sector.setAdapter(myAdaptersector);
sector.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id)
{
sector1=sector.getSelectedItem().toString();
String selectedsectortext = (String) parent.getItemAtPosition(position);
if(position > 0){
sectoridd=sectordetail.get(selectedsectortext);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Here when i choose item at same index as before it doesn't update in the spinner view.
Inside your onItemSelected of the spinner A you can set Adapter to other spinners to be update and this will work as you want.
employer.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id)
{
if(position > 0) {
employer1 = employer.getSelectedItem().toString();
employeridname = employdetail.get(employer1);
Sectorlist(employeridname);
// Update other spinners here
myAdaptersector = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1,sectorlist);
myAdaptersector.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sector.setAdapter(myAdaptersector);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Related
i have two spiners, the first spinner is an Integer array and second is an array of 2D strings.
ArrayAdapter adapter1,adapter3;
String selectedsection;
Integer selectedsem;
Integer[] semester= new Integer[] {1, 2, 3, 4, 5};
String[][] subject=new String[][]{{"m1"},{"m2"},{"m3"},{"m4"},{"m5"},{"m6"},{"m7"},{"m8"}};
adapter1 = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item, semester);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_item);
sp1.setAdapter(adapter1);
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedsem = (Integer)parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(),String.valueOf(selectedsem),Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {}
});
adapter3 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, subject[selectedsem]);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_item);
sp3.setAdapter(adapter3);
sp3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedsubject = parent.getItemAtPosition(position).toString();
Toast.makeText(getApplicationContext(),String.valueOf(selectedsem),Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), selectedsubject, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {}
});
the app crashes because it cannot find the value of selectedsem. SO, how to get the value of selectedsem from first spinner use it in second spinner.
Hope the question is understood!
The reason is since it's Integer it can be null and you are trying to use it while initializing it and nothing is yet selected you may get nullpointerexception so better give it a default value of 0 or something at initialization
and then just create adapter for second spinner after the user selects the item.
You can place your second adapter creation just under onItemSelected
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedsem = semester[parent.getSelectedItemPosition()];
//second adapter code goes here
ArrayAdapter<String> adapterNew = new ArrayAdapter<String>(YourActivity.this, android.R.layout.simple_spinner_item, subject[selectedsem]);
sp3.setAdapter(adapterNew);
Toast.makeText(getApplicationContext(),String.valueOf(selectedsem),Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {}
});
we are just updating the second spinner just after the user selects some value in(OnItemSelected) the first spinner.
Try to make the second adapter in another function and call it right after the first adapter made, I mean try to schedule the two task not asynchronous not asynchronous
This is happening because the method onItemSelected of your first spinner's OnItemSelectedListener is only called when the spinner actually gets selected by user. So, when you try to instantiate your adapter3, selectedsem is still null. To fix this, you have to put all the code where you expect selectedsem to have a value, right after it actually gets a value, that is: inside onItemSelected method. Like this:
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedsem = (Integer)parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(),String.valueOf(selectedsem),Toast.LENGTH_SHORT).show();
if (selectedsem != null) {
adapter3 = new ArrayAdapter<String>(YourActivity.this, android.R.layout.simple_spinner_item, subject[selectedsem]);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_item);
sp3.setAdapter(adapter3);
sp3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedsubject = parent.getItemAtPosition(position).toString();
Toast.makeText(getApplicationContext(),String.valueOf(selectedsem),Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), selectedsubject, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {}
});
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {}
});
}
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();
}
});
}
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) {
}
});
If the user does not select an item of the spinner how to get the default shown value of the spinner ?
For when it is clicked (selected something)
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Object item = parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
Or for what you want,
String Text = mySpinner.getSelectedItem().toString();
I used two spinners in same fragment I want when the first spinner will complete its task then only the setOnItemSelectedListener will call for the second spinner.
Issue: But the issue is setOnItemSelectedListener is called on same time for both the spinners.How can I manage that. :(
What I want to do: I want to draw a graph on page load spinner 1 will darw a different graph and spinner 2 will draw a different graphon page load.
Any help will be Appreciated.
Why do you have same setOnItemSelectedListener for both the spinners.
Have structure like this:
spinner1.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
func1();
}
#Override
public void onNothingSelected(AdapterView<?> parentView)
{
Toast.makeText(topThis, "herf", Toast.LENGTH_LONG).show();
}
});
spinner2.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
func2();
}
#Override
public void onNothingSelected(AdapterView<?> parentView)
{
Toast.makeText(topThis, "herf", Toast.LENGTH_LONG).show();
}
});
private void func1()
{
//do task of spinner1
}
private void func2()
{
//your task for second spinner
}
spinnerLeft = (Spinner) getActivity().findViewById(R.id.spinner_left);
dataAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, arrayList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerLeft.setAdapter(dataAdapter);
spinerRight = (Spinner) getActivity().findViewById(R.id.spinner_right);
rightSpinnerAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, arrayList);
rightSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinerRight.setAdapter(rightSpinnerAdapter);
That is my two spinner that I calle d on onActivityCreated.
spinerRight.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long arg3) {
// here I called a method to download a chart and showing below 1st spinner
AppGlobals.getInstance().checkSpinner = 1;
}
private void addListenerOnSpinnerLeftItemSelection() {
spinnerLeft.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long arg3) {
//here I m called the same method to download and show chart below 2nd spinner
AppGlobals.getInstance().checkSpinner = 1;
}
But the before completing the first spinner functionality the second spinner called. I have but the int value so that when both the spinner's called the int value will tell the difference.
if (AppGlobals.getInstance().checkSpinner == 1) {
chartArea = (LinearLayout) getActivity().findViewById(R.id.chart);
} else if (AppGlobals.getInstance().checkSpinner == 0) {
chartArea = (LinearLayout) getActivity().findViewById(R.id.pie_chart);
}