开发者

How to display selected Item in Spinner?

I created two Spinners to display the data in my application. In my first Spinner, the first item of the list is always displayed directly, but in the second Spinner nothing is displayed, even if I click on an item on the drop down view. Can anybody explain this behaviour?

Here is the code of the initialisation of the two spinners:

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);

The Spinner projects is the first Spinner which works correctly. projectsList is the second Spinner which doesn't work correctly. Here are the two implementations of the adapters:

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 开发者_Go百科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
        }

    }

Here I attached a picture of the problem:

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


The only thing I can suggest is to throw in a few log statements and check if allLists.size() > 0 before and after your updateSpinners call.

Also, now that I think about it. You also need to notify the spinner that the data has changed by calling BaseAdapter#notifyDataSetChanged. Otherwise it won't know that it has new data to display. So you should extend BaseAdapter and implement SpinnerAdapter. BaseAdapter will handle the implementation of registerDataSetObserver and unregisterDataSetObserver. You'll probably also need to have your updateSpinners call into your ListAdapter's notifyDataSetChanged.

Check out the ArrayAdapter source.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜