我有一个使用自定义适配器的Spinner
,其中重写了getView()
。我遇到了捕获OnItemSelected
事件的问题,我认为这与自定义适配器有关。在我的onCreate()
中,我有以下代码:
superGroupAdapter = new SuperGroupAdapter(context, R.layout.row_sg, sg_list);
sgSpinner.setAdapter(superGroupAdapter);
sgSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long id) {
Log.d(Constants.TAG, "sg spinner on item selected");
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
这是我的自定义适配器类:
public class SuperGroupAdapter extends ArrayAdapter<String> {
@Inject SharedVisualElements sharedVisualElements;
Context context;
ArrayList<String> sg_list;
public SuperGroupAdapter(Context context, int textViewResourceId, ArrayList<String> sg_list) {
super(context, textViewResourceId, sg_list);
// add this line for any class that want to use any of the singleton objects
Injector.INSTANCE.getAppComponent().inject(this);
this.context = context;
this.sg_list = sg_list;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
parent.setBackgroundColor(sharedVisualElements.backgroundColor());
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.row_sg, parent, false);
TextView label = (TextView) row.findViewById(R.id.sg_name);
label.setText(sg_list.get(position));
label.setTypeface(sharedVisualElements.font());
label.setTextColor(sharedVisualElements.primaryFontColor());
label.setGravity(Gravity.CENTER_HORIZONTAL);
return row;
}
}
当活动初始化时,我看到日志输出
但那是我最后一次看到它。无论我从微调器中选择多少次项目,它都不会再次触发。我已经四处寻找方法来解决这个问题,但都没有成功。有人可以帮忙吗?谢谢。 编辑 我还尝试将类签名更改为实现在选择项目时看到了sg spinner
OnItemSelected
,并声明侦听器作为单独的方法,如Android docs所解释的那样,但结果相同。对于这个问题,我真的很困惑。感谢任何帮助。