将项目添加到ListView的结束 [英] Adding items to the end of a ListView

查看:138
本文介绍了将项目添加到ListView的结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我在这里有一个ListView上市,我从一个WebService和数据库得到物品做这个应用程序。在这份名单中,我需要一拉刷新,当用户接近当前列表的末尾加载更多itens。无论是拉刷新和负载更itens当接近尾声正在工作。但是,添加的物品时的问题是,增加一个项到列表的顶部或端部的insetead,它只是刷新整个列表加载新项目,并排除旧的。所以,我怎么可以添加项目到年底或一ListView控件的顶部?

So, I'm doing this app where you have a ListView listing items that I get from a WebService and a Database. In this list I need a pull to refresh and load more itens when the user is near the end of the current list. Both the pull to refresh and the load more itens when near the end are working. But, the problem is when adding the items, insetead of adding a item to the top or end of the list, it just refreshes the whole list loading the new items and excluding the old ones. So, how can I add a item to the end or to the top of one ListView ?

下面一些code的帮助:

Here some code to help:

@Override
    protected void onPostExecute(Boolean result) {
        Log.d("Async 4", "post");

        SampleAdapter adapter = new SampleAdapter(getActivity());


        if (dialog.isShowing()) {
            dialog.dismiss();
        }

        for (int i = 0; i < 1; i++) {
            for (int w = 0; w < 1; w++) {
                for (int j = 0; j < 1; j++) {
                    int cont = 10;
                    cont++;
                    for (int k = 0; k < cont; k++) {
                        adapter.add(new SampleItem("" + title[k],"" + categoria_[i],"" + data_publicao[i], 1));
                        // Log.d("", "" + title[k]);
                        setListAdapter(adapter);
                    }
                }
            }
        }
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
        pullToRefreshListView.onRefreshComplete();
    }

}

这code是我尝试添加新项目。我只是调用加载数据(从一个WebService /数据库)我的异步任务,而onPostExecute被suposed添加新的项目。

This code is where I try to add new items. I just call my async task that loads the data (from a WebService/Database), and the onPostExecute is suposed to add the newer items.

如果需要从我的项目的更多code,只问,我会投入更多。先谢谢了。

If more code from my project is needed, just ask and i'll put more. Thanks in advance.

推荐答案

您的问题是,你重新创建每次多个项目fecthed适配器。

Your problem is that you are recreating the Adapter each time more items are fecthed.

adapter = new SampleAdapter(getActivity());
setListAdapter(adapter);

移动适配器的创建到您的活动的onCreate方法,并让它成为你的活动类的字段。

Move the creation of the adapter to your activity's onCreate method and make it a field of your activity class.

然后,而不是在for循环重新设置适配器

then instead of setting the adapter again in that for loop

只是通知适配器后的for循环一切都已经加入后;

just notify the adapter after the for loops after everything has been added;

adapter.notifyDataSetChanged()

这篇关于将项目添加到ListView的结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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