BaseAdapter in ListActivity isn't displaying the correct items

為{幸葍}努か 提交于 2019-12-11 16:04:14

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/4904529/baseadapter-in-listactivity-isnt-displaying-the-correct-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!