开发者

BaseAdapter in ListActivity isn't displaying the correct items

I have a ListActivity in which I am trying to display an ImageView under the List. I'm attempting to do this by creating my own BaseAdapter and using two different methods inside the getView method, one for the List and one for the ImageView. I managed to get the ImageView displayed under the List how I want it, but the problem I am having is the List isn't displayed correctly.

For some reason any item in the list that isn't on the screen until the user scrolls down gets populated with the wrong data like so:

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

What it should be is like so:

----Top Screen------
|开发者_运维百科     Item 1       |
|     Item 2       |
|     Item 3       |
|     Item 4       |
----Bottom Screen---
|     Item 5       |
|     Item 6       |
|     Image View   |

My custom 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;
    }

Thanks for the help all!


getView recycles views as you scroll. If convertView is not null, you still need to check that it is the correct type and set the correct values on it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜