ListActivity中的BaseAdapter未显示正确的项目

时间:2011-02-05 01:31:09

标签: java android adapter listactivity

我有一个ListActivity我试图在列表下显示ImageView。我试图通过创建我自己的BaseAdapter并在getView方法中使用两种不同的方法来做到这一点,一种用于List,另一种用于ImageView。我设法在列表下显示ImageView我想要的方式,但我遇到的问题是列表没有正确显示。

由于某些原因,列表中的任何项目在用户向下滚动之前不在屏幕上会被填充错误的数据,如下所示:

----Top Screen------
|     Item 1       |
|     Item 2       |
|     Item 3       |
|     Item 4       |
----Bottom Screen---
|     Item 1       |   <--Items not on the screen show
|     Item 1       |   <--as item 1 once the user scrolls
|     Image View   |   

应该是这样的:

----Top Screen------
|     Item 1       |
|     Item 2       |
|     Item 3       |
|     Item 4       |
----Bottom Screen---
|     Item 5       |
|     Item 6       |
|     Image View   |

我的自定义BaseAdapter

    private class MyCustomAdapter extends BaseAdapter {
        private static final int TYPE_ITEM = 0;
        private static final int TYPE_SEPARATOR = 1;
        private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;

        private ArrayList<HashMap<String,String>> mData = new ArrayList<HashMap<String,String>>();
        private LayoutInflater mInflater;    
        private TreeSet mSeparatorsSet = new TreeSet();

        public MyCustomAdapter() {
            mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public void addItem(final String header, final String message) {
            HashMap<String,String> item = new HashMap<String,String>();
            item.put(LINE_1, header);
            item.put(LINE_2, message);

            mData.add(item);
            notifyDataSetChanged();
        }

        public void addImageItem() {
            HashMap<String,String> item = new HashMap<String,String>();
            item.put(LINE_1, "");
            item.put(LINE_2, "");

            mData.add(item);
            mSeparatorsSet.add(mData.size() - 1);
            notifyDataSetChanged();
        }

        @Override
        public int getItemViewType(int position) {
            return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
        }

        @Override
        public int getViewTypeCount() {
            return TYPE_MAX_COUNT;
        }

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

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

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

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder vh = null;
            int type = getItemViewType(position);
            System.out.println("getView " + position + " " + convertView + " type = " + type);
            if (convertView == null) {
                vh = new ViewHolder();
                switch (type) {
                    case TYPE_ITEM:
                        convertView = mInflater.inflate(R.layout.instructions_two_row, null);
                        vh.header = (TextView) convertView.findViewById(R.id.instructions_header);
                        vh.message = (TextView) convertView.findViewById(R.id.instructions_message);
                        vh.header.setText((CharSequence) mData.get(position).get(LINE_1));
                        vh.message.setText((CharSequence) mData.get(position).get(LINE_2));
                        break;
                    case TYPE_SEPARATOR:
                        convertView = mInflater.inflate(R.layout.instructions_image, null);
                        vh.imageView = (ImageView) convertView.findViewById(R.id.instuct_image);
                        vh.imageView.setImageDrawable(getResources().getDrawable(R.drawable.instructions));
                        break;
                }
                convertView.setTag(vh);
            }
            else {
                vh = (ViewHolder) convertView.getTag();
            }           
            return convertView;
        }   
    }

    public static class ViewHolder {
        public TextView header;
        public TextView message;
        public ImageView imageView;
    }

感谢所有人的帮助!

1 个答案:

答案 0 :(得分:0)

滚动时,

getView会回收视图。如果convertView不为null,您仍需要检查它是否是正确的类型并在其上设置正确的值。