我有一个使用自定义 ArrayAdapter 设置的微调器:
private static class CustomAdapter<T> extends ArrayAdapter<String> {
public CustomAdapter(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId, objects);
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textView = (TextView) view.findViewById(android.R.id.text1);
textView.setText("");
return view;
}
它初始化如下(Spinner 微调器;语句在上面作为类变量):
this.spinner = (Spinner) findViewById(R.id.spinner1);
CustomAdapter<String> adapter = new CustomAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, new String[] {"Set Homepage"});
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
我已经实现了 OnItemSelectedListener:
public class MainActivity extends Activity implements OnItemSelectedListener{...}
并具有所需的回调:
//spinner methods
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// TODO Auto-generated method stub
//if (pos == 1){
Toast.makeText(this, "Person wants to change the homepage", Toast.LENGTH_SHORT).show();
//}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
//Toast.makeText(this, "Person wants to change the homepage", Toast.LENGTH_SHORT).show();
}
微调器的 xml:
<Spinner
android:id="@+id/spinner1"
android:layout_width="45dp"
android:layout_height="45dp"
android:background="@drawable/ic_menu_moreoverflow_holo_dark" />
问题是,无论何时从微调器中选择一个项目,都不会发生任何事情,即使在我删除了所有条件之后,如您所见。
最佳答案
OnItemSelectedListener not working for spinner
因为您只传递 Adapter 中默认选中的一项。可能您在启动应用程序时收到 toast 消息。
因此添加更多元素来检查 OnItemSelectedListener
行为。
关于android - OnItemSelectedListener 不适用于微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21845424/