【问题标题】:Spinner OnItemSelected With Custom Adapter带有自定义适配器的微调器 OnItemSelected
【发布时间】:2015-09-12 09:27:17
【问题描述】:

我有一个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 微调器

但那是我最后一次看到它。无论我从微调器中选择一个项目多少次,它都不会再次触发。我一直在寻找一种方法来捕获它,但无济于事。任何人都可以帮忙吗?谢谢。

编辑 我还尝试更改类签名以实现 OnItemSelected 并将侦听器声明为单独的方法,如 Android docs 中所述,但得到了相同的结果。

我在这个问题上真的不知所措。感谢您的帮助。

【问题讨论】:

    标签: android android-spinner android-adapter


    【解决方案1】:

    嗯,我想通了。在查看了其他一些帖子后,我突然想到,在我的测试数据中,我的微调器列表中只有一项。 OnItemSelectedListener 仅在您更改 选择时触发。

    来自OnItemSelectedListener 的 Android 文档

    只有当新选择的位置是 与先前选择的位置不同,或者如果没有 选定的项目。

    因此,当活动初始化时,它选择了位置 0 处的项目。当我点击微调器并“选择”同一个项目时,此操作不会触发该事件。活到老,学到老。

    【讨论】:

      【解决方案2】:

      我认为您在适配器 SuperGroupAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 之前缺少这个

      【讨论】:

        【解决方案3】:

        将项目添加到 sg_list 或更改 sg_list 后尝试调用 superGroupAdapter.notifyDataSetChanged()

        【讨论】:

        • 谢谢。一旦创建了 sg_list,我就从未真正更改过它,所以我认为这不是问题所在。我试了一下以防万一,但没有任何改变。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多