简体   繁体   中英

Reaching the selected item's value on spinner

I am working on a registration system. I need to create faculties with getting user input then assign students to these faculties. I should display the faculty informations when a student's name is selected. I am using an arraylist to keep faculty inputs. Also I am using a spinner to select faculty. But somehow I can't reach the faculty name when a spinner item selected.It says null everytime. Here are parts from my code.

   public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    
        
        ArrayList<String> facultyArr = new ArrayList<String>();
    .
    .
    .
            
            
     protected void onCreate(Bundle savedInstanceState) {
   spnF.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) this);
                       

 ArrayAdapter<String> af = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, facultyArr);
                    af.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    spnF.setAdapter(af);
            spnF.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            
                            pos = parent.getSelectedItemPosition();
                        }
            
                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {
            
                        }
            
                    });
                String facOfStu = facultyArr.get(pos);
    .
    .
    .
    .}//end of onCreate
        

How can I get the selected faculty from a initially null arrayList? Thanks for reading to all of you.

You need to take a reactive approach.

        spnF.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        //You can get the faculty when the user selects it.
                        String faculty = facultyArr.get(position);
                        
                    }
                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {}
                });

You can also get the specific faculty when you know at least one exists.

//Simulate filling the faculty list.
facultyArr.add("This guy");
facultyArr.add("That guy");
facultyArr.add("The guy");
//Notify the adapter it needs to relook at its data.
af.notifyDataSetChanged();

String faculty = facultyArr.get(0);

Calling String facOfStu = facultyArr.get(pos); at the end of your onCreate will result in an IndexOutOfBoundsException if facultyArr empty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM