BaseAdapter在ListActivity显示不正确的项目 [英] BaseAdapter in ListActivity isn't displaying the correct items

查看:279
本文介绍了BaseAdapter在ListActivity显示不正确的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ListActivity 中,我试图列表下显示的ImageView 。我试图通过创建要做到这一点我自己的 BaseAdapter ,并使用 getView 方法中两种不同的方法,一个用于列表和一个用于的ImageView 。我设法让的ImageView 我多么希望它的列表下显示,但我有问题是列表显示不正常。

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   |

我的自定义 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 为您滚动回收的意见。如果convertView不为空,你还需要检查它是否是正确的类型,并在其上​​设置正确的值。

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.

这篇关于BaseAdapter在ListActivity显示不正确的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆