android listview试图设置新适配器但视图不刷新

时间:2014-10-03 12:40:05

标签: java android listview android-listview android-arrayadapter

我正在构建一个加载新闻帖子的应用。 我有一个自定义适配器,当应用程序启动时,我将listview设置为适配器并用数据填充它。 我对数据来源​​的问题,第一个是“热门新闻”和第二个“近期新闻”,用户可以在点击之间切换。 默认为“最近新闻”。当用户点击热门新闻时,我清除适配器和arrayList,用新数据填充它并将其附加到新的列表视图。

我的问题是,当我这样做时,屏幕上没有显示任何内容,只有当我开始滚动屏幕再次填充数据时。 如何避免这种情况,然后用户点击“热门新闻”并且看不到列表视图?

这是和适配器类:

@Override
public int getItemViewType(int position) {
    if (position == 0) {
        return 0;
    } else if (position == 1)
        return 1;
    if (position > 1 && position < 5) {
        return 2;
    } else {
        return 1;
    }

}

@Override
public int getViewTypeCount() {
    return 3;
}


@Override
public View getView(final int position, final View convertView,
        ViewGroup parent) {
    View row = convertView;
    final ViewHolder holder;
    int type = getItemViewType(position);
    // if first time you load this line
    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        holder = new ViewHolder();

        if (type == 0) {
            row = inflater.inflate(R.layout.gap_between_main_post_to_top,
                    null, true);
            row.setTag("0");
            return row;
        }

        if (type == 1 || type == 3) {
            row = inflater.inflate(R.layout.new_main_post, null, true);
            holder.title = (TextView) row
                    .findViewById(R.id.textViewMainTitle);
            holder.pubDate = ((TextView) row
                    .findViewById(R.id.mainPostDate));
            holder.postImage = (ImageView) row
                    .findViewById(R.id.imageViewMain);
            holder.postLikes = ((TextView) row
                    .findViewById(R.id.postLikesMain));
            holder.postCommunts = ((TextView) row
                    .findViewById(R.id.postCommuntsMain));
            holder.postShares = ((TextView) row
                    .findViewById(R.id.postSharesMain));
            holder.like_share_communt = (LinearLayout) row
                    .findViewById(R.id.share_like_communt);
            holder.postLikesImage = (ImageView) row
                    .findViewById(R.id.imageViewLike);

            row.setTag(holder);
        } else if (type == 2) {
            row = inflater.inflate(R.layout.new_main_sub_post, null, true);
            holder.title = (TextView) row.findViewById(R.id.textViewSub1);
            holder.pubDate = (TextView) row.findViewById(R.id.postDateSub);
            holder.postImage = (ImageView) row
                    .findViewById(R.id.imageViewSub1);
            holder.like_share_communt = (LinearLayout) row
                    .findViewById(R.id.share_like_communt);
            holder.postLikes = ((TextView) row.findViewById(R.id.postLikes));
            holder.postCommunts = ((TextView) row
                    .findViewById(R.id.postComments));
            holder.postShares = ((TextView) row
                    .findViewById(R.id.subPostShares));
            holder.postLikesImage = (ImageView) row
                    .findViewById(R.id.imageViewLike);
            row.setTag(holder);
        }

    } else {
        if(row.getTag().toString().equals("0"))
            return row;
        else
        holder = (ViewHolder) row.getTag();
    }

    .......

这是onClick方法:

private OnClickListener changeNewsSourceListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        if (v == popularNews) {
            adapter.clear();
            postsArray.clear();

            sort = "popularity";
            pageNumber = 1;
            addItems();

        }

    }
};

这是适配器填充数据的地方(addItems方法):

    public void addItems() {

    new RoboAsyncTask<ArrayList<Post>>(this) {
        protected void onPreExecute() {

        }

        public ArrayList<Post> call() throws Exception {

            try {
                String url = context.getResources().getString(
                        R.string.servicesUrl);
                url += "?page=" + pageNumber + "&sort=" + sort;
                return DataPosts.getInstance(context).getPosts(url);
            } catch (Throwable e) {
                Log.i("exption", e.toString());
                e.printStackTrace();
                throw new Exception(e.getMessage());
            }

        }

        protected void onSuccess(java.util.ArrayList<Post> result) {
            swipeLayout.setRefreshing(false);
            postsArray.addAll(result);
            mainListView.removeAllViewsInLayout();
            adapter.notifyDataSetChanged();

        };

        protected void onException(Exception e)

        {
            Log.i("exption", e.toString());
        }
    }.execute();

}

0 个答案:

没有答案