BaseAdapter中的Android getView()未调用(但调用了getCount())

时间:2017-09-04 07:57:59

标签: android

没有错误,但ListView为空。我已经实现了getCount(),它返回了我的ArrayList中正确数量的项目。 ListView是可见性。 我在ListView上调用了setListAdapter,xml是match_parent。

这是我的片段。

 mNewsList = infoBean.getNews();
        if (mNewsList != null) {
            mNewsAdapter = new NewsAdapter();
            mListView.setAdapter(mNewsAdapter);
            mNewsAdapter.notifyDataSetChanged();
        }
    } else {//load more
        List<HomeBean.NewsBean> news = infoBean.getNews();
        if (mNewsList != null) {
            mNewsList.addAll(news);
            mNewsAdapter.notifyDataSetChanged();
        }

这是我的适配器:

private class NewsAdapter extends BaseAdapter {

    @Override
    public int getCount() {
        return mNewsList.size();
    }

    @Override
    public HomeBean.NewsBean getItem(int position) {
        return mNewsList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(MyApplication.getmContext(), R.layout.item_list, null);

            holder = new ViewHolder();
            holder.ivIcon = convertView.findViewById(R.id.iv_item);
            holder.tvTitle = convertView.findViewById(R.id.tv_item_list_title);
            holder.content = convertView.findViewById(R.id.tv_item_list_content);
            holder.tvTime = convertView.findViewById(R.id.tv_time);
            holder.author = convertView.findViewById(R.id.tv_author);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tvTitle.setText(getItem(position).getTitle());
        holder.content.setText(getItem(position).getContent());
        SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd");
        String itemTime = dataFormat.format(getItem(position).getCreate_at() * 1000);
        holder.tvTime.setText(itemTime);
        holder.author.setText(getItem(position).getSection_name());
        if (getItem(position).getCover_pic().startsWith("http:")) {
            picUrl = getItem(position).getCover_pic();
        } else {
            picUrl = RBConstants.SERVER_PIC + getItem(position).getCover_pic();
        }
        HttpLoader.getInstance(MyApplication.getmContext()).display(holder.ivIcon, picUrl);

        return convertView;
    }
}

这是我的持有人。

class ViewHolder {
    public ImageView ivIcon;
    public TextView tvTitle;
    public TextView tvTime;
    public TextView content;
    public TextView author;
}

2 个答案:

答案 0 :(得分:0)

这是因为mListView.setAdapter(mNewsAdapter);永远不会被执行,因为在调用mNewsAdapter.notifyDataSetChanged();之后不需要立即调用mListView.setAdapter(mNewsAdapter);

答案 1 :(得分:0)

ListView已过时,请切换到RecyclerView