如何在Spinner中显示所选项?

时间:2010-06-18 22:38:17

标签: java android

我创建了两个Spinners来显示我的应用程序中的数据。在我的第一个Spinner中,列表的第一项始终直接显示,但在第二个Spinner中,即使我单击下拉视图中的项目,也不会显示任何内容。任何人都可以解释这种行为吗?

以下是两个微调器的初始化代码:

projects = new Spinner(lexs);
projectAdapter = new ProjectAdapter();
projects.setAdapter(projectAdapter);
projects.setMinimumWidth(250);
projects.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
   updateSpinners(position);                    
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
   // do nothing    
}

});

projectsList = new Spinner(lexs);
projectsList.setMinimumWidth(250);
listAdapter = new ListAdapter();
projectsList.setAdapter(listAdapter);
projectsListLayer.addView(projectsList);

Spinner项目是第一个正常工作的Spinner。 projectsList是第二个无法正常工作的Spinner。以下是适配器的两种实现:

private class ProjectAdapter implements SpinnerAdapter {

        @Override
        public View getDropDownView(int position, View arg1, ViewGroup arg2) {
            TextView text = new TextView(lexs);
            text.setText(allProjects.get(position).getName());
            return text;
        }

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

        @Override
        public Object getItem(int position) {
            return allProjects.get(position);
        }

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

        @Override
        public int getItemViewType(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View arg1, ViewGroup arg2) {
            TextView text = new TextView(lexs);
            text.setText(allProjects.get(position).getName());
            return text;
        }

        @Override
        public int getViewTypeCount() {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @Override
        public boolean isEmpty() {
            if (allProjects.size() == 0) {
                return true;
            } else {
                return false;
            }
        }

        @Override
        public void registerDataSetObserver(DataSetObserver arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void unregisterDataSetObserver(DataSetObserver observer) {
            // TODO Auto-generated method stub

        }

    }

    private class ListAdapter implements SpinnerAdapter {

        @Override
        public View getDropDownView(int position, View view, ViewGroup parent) {
            TextView text = new TextView(lexs);
            text.setText(allLists.get(position).getName());
            return text;
        }

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

        @Override
        public Object getItem(int position) {
            return allLists.get(position);
        }

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

        @Override
        public int getItemViewType(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView text = new TextView(lexs);
            text.setText(allLists.get(position).getName());
            return text;
        }

        @Override
        public int getViewTypeCount() {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public boolean hasStableIds() {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean isEmpty() {
            if (allLists.size() == 0) {
                return true;
            } else {
                return false;
            }
        }

        @Override
        public void registerDataSetObserver(DataSetObserver observer) {
            // TODO Auto-generated method stub
        }

        @Override
        public void unregisterDataSetObserver(DataSetObserver observer) {
            // TODO Auto-generated method stub
        }

    }

这里我附上了问题的图片:

http://www.freeimagehosting.net/image.php?7684c157b8.png

1 个答案:

答案 0 :(得分:1)

我唯一可以建议的是在allLists.size() > 0来电之前和之后输入一些日志语句并检查是否updateSpinners

此外,现在我考虑一下。您还需要通过调用BaseAdapter#notifyDataSetChanged通知微调器数据已更改。否则它将不知道它有新数据要显示。因此,您应该扩展BaseAdapter并实施SpinnerAdapterBaseAdapter将处理registerDataSetObserverunregisterDataSetObserver的实施。您可能还需要将updateSpinners电话转入ListAdapter的{​​{1}}。

查看ArrayAdapter来源。