2

我有一个数组适配器的自定义实现,我在 Spinner 中使用它来为我的对象获取自定义布局。

使用自定义数组适配器时,我会出现带有微调器选项的对话框,但没有一个可以选择。我可以使用后退按钮关闭对话框。

OnItemSelected 未被调用。(当我使用后退按钮时,也不会调用 OnNothingSelected)。

这是一些代码:

自定义适配器:

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;
    }
}

微调器初始化:

    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
        }
    });
4

1 回答 1

1

我的解决方法是从头开始编写一个新的 ArrayAdapter 和一个新的布局。无论出于何种原因,这似乎都做到了。虽然我根本没有改变我的方法论。

于 2012-07-05T19:33:27.467 回答