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

时间:2013-07-16 12:20:28

标签: 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)。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

    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

        }
    });