更新后Android ArrayAdapters convertviews标记为null

时间:2012-02-08 13:31:16

标签: android listview android-arrayadapter

我正在尝试更新我的自定义ArrayAdapter,但在调用updateListArray之后调用了getView并且convertview不为null但是标记为null,这给了我错误。如果我进行额外的空检查并设置新的视图,它只显示错误的内容。 我根本无法弄清楚为什么这会给我带来问题,我在代码中有以下内容:

    public class CustomAdapter extends ArrayAdapter<Item> {
        private ArrayList<Item> mListItems;

        public CustomAdapter (Context context, int rowResourceId, ArrayList<Item> items) {
            super(context, rowResourceId, items);
            mListItems = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if(convertView == null) {
                // setup holder
                holder = new ViewHolder();

                convertView = mInflater.inflate(R.layout.list_row, null);

                holder.mEventStart = (TextView) convertView.findViewById(R.id.row_date);
                holder.mTitle = (TextView) convertView.findViewById(R.id.row_artist);
                holder.mSubTitle = (TextView) convertView.findViewById(R.id.row_description);

                holder.mImage = (ImageView) convertView.findViewById(R.id.row_artist_image);
                holder.mScene = (ImageView) convertView.findViewById(R.id.row_scene_image);
                holder.mStatus = (ImageView) convertView.findViewById(R.id.row_new);
                holder.mRowParent = (View) convertView.findViewById(R.id.row_parent);

            } else {
                // get existing row view
                holder = (ViewHolder) convertView.getTag();
            }

            //setup row view content
            if(!mListItems.isEmpty()) {
//set content
            }
            return convertView;
        }

        public void updateListArray(ArrayList<Item> list) {
            this.clear();
            for(Item item : list) {
                this.add(item);
            }
            mListItems = list;
            notifyDataSetChanged();
        }

1 个答案:

答案 0 :(得分:0)

除非您使用了setTag(),否则

getTag()将为null。如果你删除对getTag()的调用

,它可能会按你的意愿工作