[英]Android Spinner OnItemSelected not firing when using custom child of ArrayAdapter
I have a custom implementation of an Array Adapter that I'm using in a Spinner to get a custom layout for my objects. 我有一个自定义实现的数组适配器,正在微调器中使用它来获取对象的自定义布局。
With the Custom array adapter in use, I have dialog appear with the spinner options, but none of them can be selected. 使用“自定义阵列适配器”时,出现带有微调器选项的对话框,但是无法选择它们。 I can use the back button to dismiss the dialog.
我可以使用“后退”按钮关闭对话框。
OnItemSelected is not being called. 不调用OnItemSelected。 (When I use the back button, OnNothingSelected is not called either).
(当我使用后退按钮时,也不会调用OnNothingSelected)。
Here's some code: 这是一些代码:
Custom Adapter : 定制适配器:
public class AboutSettingsServerAdapter extends ArrayAdapter <ServerItem> {
private ArrayList<ServerItem> mServers;
private Context mContext;
public AboutSettingsServerAdapter(Context context, int resource, ArrayList<ServerItem> serverItems) {
super(context, resource, serverItems);
mContext = context;
mServers = serverItems;
}
public int getCount(){
return mServers.size();
}
public ServerItem getItem(int position){
return mServers.get(position);
}
public long getItemId(int position){
return position;
}
public View getView (int position, View convertView, ViewGroup parent) {
return getAdapterView(position, convertView, parent);
}
public View getDropDownView (int position, View convertView, ViewGroup parent) {
return getAdapterView(position, convertView, parent);
}
public View getAdapterView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.spinner_item, null);
}
ServerItem item = mServers.get(position);
if (item!= null) {
TextView titleView = (TextView) view.findViewById(R.id.server_settings_title);
TextView subtitleView = (TextView) view.findViewById(R.id.server_settings_subtitle);
if (titleView != null) {
titleView.setText(item.title);
}
if (subtitleView != null) {
subtitleView.setText(item.url + "/" + item.path_extension);
}
}
return view;
}
}
Spinner initialization : 微调器初始化:
AboutSettingsServerAdapter adapter = new AboutSettingsServerAdapter(this, R.layout.module_about_server_spinner_item, servers);
adapter.setDropDownViewResource(R.layout.spinner_item);
adapter.notifyDataSetChanged();
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int index, long id) {
mSelectedServer = servers.get(index);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
My fix was to start from scratch and write a new ArrayAdapter and a new layout. 我的解决方法是从头开始,并编写新的ArrayAdapter和新的布局。 For whatever reason this seemed to do it.
无论出于何种原因,这似乎都可以做到。 Though I did not change my methodology at all.
尽管我根本没有改变我的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.