Search code examples
androidarraylistspinneronitemselectedlistener

Display another arraylist element in textview using onItemSelected of a spinner


I have 2 arraylists, 1 is use to display the elements to the spinner and another one is use to display on a textview when one of the element from spinner is selected.

Example:

0---a---football

1---b---badminton

2---c---basketball

"a,b,c" are the elements in arraylist1; "football, badminton, basketball" are the elements in arraylist2; "0,1,2" are the index for both arraylists.The index of elements on both of the arraylists has already arranged properly as shown above.

What I want to do now is to let the spinner to display "a,b,c". When I select "b" in the spinner, the textview will show me "badminton".

What should I write in the onItemSelected of the spinner?Any idea for this?


Solution

  • spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, 
        int position, long id) {
        switch(position){
          case 0: 
          textView1.setText(sportsList.get(0));
          break;
          case 1:
          textView2.setText(sportsList.get(1));
          break;
          case 2: 
          textView3.setText(sportsList.get(2));
           break;
        }
       }
    });