anybody can help! the spinner listener doesn't work by this fragment.
it's never called
I have tried to implement an AdapterView.OnItemSelectedListener
I try to implement the spinner in Fragment I don't know if that really matter
Java code
'''
Spinner mSpinner = (Spinner) view.findViewById(R.id.usernameSpinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, users);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adapter);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, "onItemSelected: " + position);
//its never called
}
#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 update the spinner view by calling notifyDataSetChanged(). But its not updating.
The below is my Activity:
public class DisplayArchive2 extends AppCompatActivity {
List<String> yearslist;
Spinner spinner;
ArrayAdapter<String> dataAdapter;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.archive2);
spinner = (Spinner) findViewById(R.id.year_spinner);
yearslist = new ArrayList<String>();
yearslist.add("2017");
yearslist.add("2016");
yearslist.add("2015");
yearslist.add("2014");
yearslist.add("2013");
dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,yearslist);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
yearslist.clear();
yearslist.add("2010");
yearslist.add("2009");
yearslist.add("2008");
yearslist.add("2007");
yearslist.add("2006");
dataAdapter.notifyDataSetChanged();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
The list of years is not changing on selecting any year.
It looks like below
Try this use dataAdapter2.setNotifyOnChange(true); its working fine i have tested it my device
void setNotifyOnChange (boolean notifyOnChange)
Control whether methods that change the list (add(T), addAll(Collection), addAll(Object[]), insert(T, int), remove(T), clear(), sort(Comparator)) automatically call notifyDataSetChanged(). If set to false, caller must manually call notifyDataSetChanged() to have the changes reflected in the attached view. The default is true, and calling notifyDataSetChanged() resets the flag to true.
CODE
List<String> yearslist;
Spinner spinner;
ArrayAdapter<String> dataAdapter2;
spinner = (Spinner) findViewById(R.id.year_spinner);
yearslist = new ArrayList<String>();
yearslist.add("2017");
yearslist.add("2016");
yearslist.add("2015");
yearslist.add("2014");
yearslist.add("2013");
dataAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,yearslist);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter2);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
} else {
yearslist.clear();
yearslist.add("2010");
yearslist.add("2009");
yearslist.add("2008");
yearslist.add("2007");
yearslist.add("2006");
dataAdapter2.setNotifyOnChange(true);
dataAdapter2.notifyDataSetChanged();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
As per my understanding. You are notifying the adapter inside onItemSelected(). In case of Spinner adapter initialization onItemSelected() will get called once for first time . So the list you will get in Spinner is always the new one(i.e 2006-2010) never the first one(i.e 2013-2017).
So if you do notify adapter on some other action or maybe avoid the first call to onItemSelected() your code will work fine .
spinner is not populating based on the another spinner selection , I have been on this for hours , still can't get the issue , No error in LOGCAT. Thanks in advance.
This is my code
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, SPINNERCOUNTRY);
spinnerCountry = (MaterialBetterSpinner) findViewById(R.id.spinnerState);
spinnerCountry.setAdapter(adapter);
spinnerCity = (Spinner) findViewById(R.id.spinnerCity);
spinnerCountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {
adapterView.getItemAtPosition(pos);
if (pos == 0) {
ArrayAdapter<CharSequence> adapter = ArrayAdapter
.createFromResource(SpinnerStateCitySelectorActivity.this, R.array.city_Tamil_Nadu,
android.R.layout.simple_spinner_dropdown_item);
spinnerCity.setAdapter(adapter);
} else if (pos == 1) {
ArrayAdapter<CharSequence> adapter = ArrayAdapter
.createFromResource(SpinnerStateCitySelectorActivity.this, R.array.city_Kerela,
android.R.layout.simple_spinner_dropdown_item);
spinnerCity.setAdapter(adapter);
} else if (pos == 2) {
ArrayAdapter<CharSequence> adapter = ArrayAdapter
.createFromResource(SpinnerStateCitySelectorActivity.this, R.array.city_Karnataka,
android.R.layout.simple_spinner_dropdown_item);
spinnerCity.setAdapter(adapter);
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
You may try it with value of selected item, using onItemSelectedListner
yourMainSpin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String spinnerValue = yourMainSpin.getSelectedItem().toString();
if (spinnerValue.equals("requierdString")) {
subSpinner.setAdapter(yourAdapter);
}else if (spinnerValue.equals("requierdString")){
subSpinner.setAdapter(yourAdapter);
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Hope this will Help you.
User onItemClickListener instead of setOnItemSelectedListener
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
}