android - 带有自定义适配器的微调器显示所选项目 Android

标签 android android-layout android-spinner android-adapter android-adapterview

在我的程序中使用带有自定义适配器的微调器。适配器是:

private class lenguageSpinnerAdapter extends BaseAdapter implements
        SpinnerAdapter {

    private ArrayList<String> lenguages;

    public lenguageSpinnerAdapter(Context context,
            ArrayList<String> lenguages) {
        this.lenguages = lenguages;
    }

    public int getCount() {
        return lenguages.size();
    }

    public Object getItem(int position) {
        return lenguages.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            // This a new view we inflate the new layout
            LayoutInflater inflater = (LayoutInflater) context
                    .getApplicationContext().getSystemService(
                            Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.lenguage_item_layout,
                    parent, false);
        }

        TextView lenguage = (TextView) convertView
                .findViewById(R.id.textViewLenguage);
        lenguage.setText(lenguages.get(position).toString());


        return convertView;
    }

}

我在 Activity 中使用此适配器:

ArrayList<String> spinnerArray = new ArrayList<String>();
    spinnerArray.add("IT");
    spinnerArray.add("EN");
    spinnerArray.add("PR");

    final lenguageSpinnerAdapter lenguageAdapter = new lenguageSpinnerAdapter(
            getContext(), spinnerArray);
    spinnerLenguage.setAdapter(lenguageAdapter);

问题是这个微调器总是显示三种语言,但我希望他只显示未选择的语言(如果选择了 IT 则为 EN 和 PR,或者如果选择了 EN 则为 IT 和 PR)。有什么想法吗?

最佳答案

    spinnerLenguage.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int position, long arg3) {

            // Build your new array
            // Build your new adapter
            // Set your new adapter to spinnerLenguage

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

关于android - 带有自定义适配器的微调器显示所选项目 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17676362/

相关文章:

java - 选择editText时如何隐藏部分布局

java - BaseExpandableListAdapter 以编程方式添加子项

php - 如何从 JSON 编码的字符串中修剪换行符?

java - 多次调用 onFocusChange 导致 "hasFocus"变量不可用

java - 如何从微调器的项目中调用 Activity

android - 为什么我的微调器看起来不像微调器?

安卓 Espresso : How to match text on custom spinner selected item layout?

java - 如何删除一个父节点及其所有子节点?

android - PrimaryDark 未在 android 中设置 StatusBar 颜色

android - 如何在 Android 中给两个线性布局之间的空间