具有自定义数组适配器的微调器不允许选择项目

时间:2014-09-09 04:54:27

标签: android android-arrayadapter android-spinner

我为微调器使用自定义数组适配器。但是,在下拉列表中选择项目时,下拉列表会保留在那里,并且微调器不会更新,这是不正常行为(与使用带字符串的通用数组适配器相比)。这是自定义类。我错过了什么吗?感谢。

public class CalendarNameAdapter extends ArrayAdapter<AgendaLogic.ExternalCalendarInfo> {
    Context mContext ;
    ArrayList<AgendaLogic.ExternalCalendarInfo> mCalendarLayers;


    public CalendarNameAdapter(Context context, int resource, ArrayList<AgendaLogic.ExternalCalendarInfo> objects) {
        super(context, resource, objects);
        mContext = context;
        mCalendarLayers = objects;
    }



    @Override
    public void add(AgendaLogic.ExternalCalendarInfo object) {
        mCalendarLayers.add(object);
    }

    @Override
    public int getCount() {
        return mCalendarLayers.size();
    }

    private class ViewHolder{
        Button button;
        TextView textView;
        public ViewHolder(Button _btn, TextView _tv){
            button = _btn;
            textView = _tv;
        }
    }

    public View getView(int position, View convertView, final ViewGroup parent) {
        View view =convertView;
        if (view==null){
            view =  View.inflate(mContext,R.layout.li_calendar_display, null);
            Button button = (Button) view.findViewById(R.id.calColor);
            TextView name = (TextView) view.findViewById(R.id.calName);
            view.setTag(new ViewHolder(button, name));

        }
        ViewHolder viewHolder = (ViewHolder) view.getTag();
        viewHolder.button.setBackgroundColor(mCalendarLayers.get(position).color);
        viewHolder.textView.setText(mCalendarLayers.get(position).name);

        return  view;
    }

    @Override
    public View getDropDownView(final int position, View convertView, final ViewGroup parent) {
        final View view = getView(position, convertView, parent);

        return  view;
    }



}

这是用法,首先使用模拟数据初始化微调器。

    public void initSpinner(){
        CalendarNameAdapter exteralCalAdapter = createAdapterWithString(this, "temp calendar");
        mExternalSpinner.setAdapter(exteralCalAdapter);

    }

    public   CalendarNameAdapter createAdapterWithString(Context context, String display) {
        ArrayList<AgendaLogic.ExternalCalendarInfo> list = new ArrayList<AgendaLogic.ExternalCalendarInfo>();
        list.add(new AgendaLogic.ExternalCalendarInfo(-1, display, 0xaabbcc));
        CalendarNameAdapter dataAdapter = new CalendarNameAdapter(context,
                0, list); //don't care about resource file, since we always use custom file, hence 0
        return dataAdapter;
    }

当我已经加载了我需要的数据时,我将它添加到适配器:

private void setCalendarInfoToSpinner(ArrayList<AgendaLogic.ExternalCalendarInfo> calList, Spinner spinner) {
    CalendarNameAdapter adapter = (CalendarNameAdapter) spinner.getAdapter();
    adapter.clear();
    for (AgendaLogic.ExternalCalendarInfo info: calList)
        adapter.add(info);
    adapter.notifyDataSetChanged();

}

1 个答案:

答案 0 :(得分:0)

我最后回答了自己的问题,以了解以后谁需要它。解决方案是:检查列表项的布局文件。如果有按钮,收音机,复选框,等等。将其可聚焦性设置为false,以便当您点击该项目时,列表将获得点击,而不是该子视图。

android:focusable="false"

对于类似问题的另一个答案是信用,但我现在似乎无法找到它。