2017-05-26 68 views
-2
holder.spinner_teacher_name=(Spinner) rowView.findViewById(R.id.spinner_teacher_name); 

    holder.spinner_teacher_name=(Spinner) rowView.findViewById(R.id.spinner_teacher_name); 

    arrayAdapter=new ArrayAdapter<String>(rowView.getContext(), android.R.layout.simple_spinner_item,teacher_name); 
    arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    holder.spinner_teacher_name.setAdapter(arrayAdapter); 
    holder.spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

    Toast.makeText(rowView.getContext(), "You Clicked "+class_room[position], Toast.LENGTH_SHORT).show(); 
     } 
    }); 

回答

0

你應該選擇使用監聽器,而不是當用戶selectsdrop-down項目時,Spinner對象收到on-item-selected事件點擊收聽

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
     { 
      public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) 
      { 
      String selected_item = parent.getItemAtPosition(pos).toString(); 
      Log.d("TAG", "_log : item_selected : " + selected_item); 
      } 

      public void onNothingSelected(AdapterView<?> parent) 
      { 

      } 
     }); 
+0

先生親切,我想知道這條線 –

+0

Log.d一些細節( 「TAG」,「_log:item_selected :「+ selected_item); –

+0

它將打印您從微調器中選擇的項目;) –

0

要定義spinnerselection事件處理程序,請實現AdapterView.OnItemSelectedListener接口和相應的onItemSelected()回調方法。

請參閱documentation

用途:

holder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
    @Override 
    public void onItemSelected(AdapterView<?> parent, View v, int position, long id) { 

     Toast.makeText(rowView.getContext(), "You Clicked " + class_room[position], Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onNothingSelected(AdapterView<?> adapterView) { 

    } 
}); 

相反的:

holder.spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

     Toast.makeText(rowView.getContext(), "You Clicked "+class_room[position], Toast.LENGTH_SHORT).show(); 
    } 
}); 
+0

沒有解決先生。同樣的問題 –