Android counter not starting onItemSelected

In my application I use spinner, it shows with spinner.performClick() . After many attempts, I noticed that my code does not run the onItemSelected method . Then I tried to add the @Override annotation, but it returns as an error and says delete this annotation. here is this code:

 Spinner colorSpinner = new Spinner(this); ColorFriendsSpinnerAdapter adapter = new ColorFriendsSpinnerAdapter(getApplicationContext(), liste, R.layout.color_dropdown, new String[] { "Icon" }, new int[] { R.id.colorDropdown }); //adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); colorSpinner.setAdapter(adapter); colorSpinner.performClick(); colorSpinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) { editColorXml(position); } public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); 

Thanks for any help and sorry for my english.

+4
source share
2 answers

Sample ItemSelectedListner; no need to redefine annotation.

 spinner.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub } public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); 

set the listener before colorSpinner.performClick() . In this code, the spinner will be pressed at the moment when the listener is not installed.

+1
source

Try this code.

 final List<String> list=new ArrayList<String>(); list.add("Item 1"); list.add("Item 2"); list.add("Item 3"); list.add("Item 4"); list.add("Item 5"); final Spinner sp1= (Spinner) findViewById(R.id.spinner1); ArrayAdapter<String> adp1 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list); adp1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); sp1.setAdapter(adp1); sp1.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getBaseContext(), list.get(position), Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); 
0
source

Source: https://habr.com/ru/post/1381951/


All Articles