Android:保存ListView项目并加载/添加更多内容

时间:2014-01-26 22:44:51

标签: java android listview parse-platform onscroll

当我滚动到底部并且添加它们到当前的那些时,我正在尝试在ListView中加载一些项目。它运行良好,但最近我发现它再次加载所有对象,只有更多项目。我正在使用Parse.com。我发现,Parse.com提供了自己的自定义ParseQueryAdapter,它包含getNextPageView()方法。它会加载移动项目,但仅在用户单击下面的按钮时才会加载。我还尝试了.setSkip(someNumber)方法,但它只删除/隐藏了顶部的someNumber个项目。所以我仍然希望使用OnScrollListener并且真的加载更多而不是再加载+更多。

你有什么想法,怎么做?

检查我的代码:

public void updateData() {
         mListView = (ListView)getView().findViewById(R.id.animal_list);     
   final ParseQuery<Animal> query = ParseQuery.getQuery(Animal.class);


    query.setCachePolicy(CachePolicy.NETWORK_ONLY);
    query.orderByAscending("animal");


    query.setLimit(mListView.getCount() + 5);


    query.findInBackground(new FindCallback<Animal>() {

        @Override
          public void done(List<Animal> animals, ParseException error) {

              if(animals != null){
                  mAdapter.clear();

                mProgressBar = (ProgressBar) getView().findViewById (R.id.loading_animals);
               mProgressBar.setVisibility(View.INVISIBLE);
             RelativeLayout footie = (RelativeLayout) getView().findViewById(R.id.footerview);  
            footie.setVisibility(View.VISIBLE);

            for (int i = 0; i < animals.size(); i++) {


                      mAdapter.add(animals.get(i));
                    ArrayList<Animal> animal = new ArrayList<Animal>();
                    animal.add(animals.get(i));






                  }  

              }  
            }
         }); }






     private class RemoteDataTask extends AsyncTask<Void, Void, Void> {


         @Override
            protected void onPreExecute() {
                super.onPreExecute();  }

         @Override
            protected Void doInBackground(Void... params) {

                return null;



         }


         @Override
            protected void onPostExecute(Void result) {


               mListView = (ListView) getView().findViewById(R.id.animal_list);

               mEditText = (EditText) getView().findViewById(R.id.search_animal);

               mAdapter = new AnimalAdapter(getActivity(), new ArrayList<Animal>());

               mListView.setVisibility(View.VISIBLE);
               mListView.setTextFilterEnabled(true);
               mListView.setAdapter(mAdapter);




               mListView.setOnScrollListener(new OnScrollListener() {

                    @Override
                    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                            int totalItemCount) {
                        final int lastItem = firstVisibleItem + visibleItemCount;
                        if(lastItem == totalItemCount) {




                        if (mListView.getCount() > 20) {

                            RelativeLayout footie = (RelativeLayout) getView().findViewById(R.id.footerview);

                            mListView.removeFooterView(footie);

                              }





                            else{

                                    updateData();
                                }

                            }

                        }

                    }

提前致谢

1 个答案:

答案 0 :(得分:1)

尝试清除Adapter,然后将其设置为ListView,如下所示:

public void updateData() {
         mListView = (ListView)getView().findViewById(R.id.animal_list);     
   final ParseQuery<Animal> query = ParseQuery.getQuery(Animal.class);


    query.setCachePolicy(CachePolicy.NETWORK_ONLY);
    query.orderByAscending("animal");


    query.setLimit(mListView.getCount() + 5);


    query.findInBackground(new FindCallback<Animal>() {

        @Override
          public void done(List<Animal> animals, ParseException error) {

              if(animals != null){
                  mAdapter.clear();

                mProgressBar = (ProgressBar) getView().findViewById (R.id.loading_animals);
               mProgressBar.setVisibility(View.INVISIBLE);
             RelativeLayout footie = (RelativeLayout) getView().findViewById(R.id.footerview);  
            footie.setVisibility(View.VISIBLE);
                    mAdapter.clear();///////HERE

            for (int i = 0; i < animals.size(); i++) {


                      mAdapter.add(animals.get(i));
                    ArrayList<Animal> animal = new ArrayList<Animal>();
                    animal.add(animals.get(i));






                  }  

              }  
            }
         }); }






     private class RemoteDataTask extends AsyncTask<Void, Void, Void> {


         @Override
            protected void onPreExecute() {
                super.onPreExecute();  }

         @Override
            protected Void doInBackground(Void... params) {

                return null;



         }


         @Override
            protected void onPostExecute(Void result) {


               mListView = (ListView) getView().findViewById(R.id.animal_list);

               mEditText = (EditText) getView().findViewById(R.id.search_animal);

               mAdapter = new AnimalAdapter(getActivity(), new ArrayList<Animal>());

               mListView.setVisibility(View.VISIBLE);
               mListView.setTextFilterEnabled(true);
               mListView.setAdapter(mAdapter);




               mListView.setOnScrollListener(new OnScrollListener() {

                    @Override
                    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                            int totalItemCount) {
                        final int lastItem = firstVisibleItem + visibleItemCount;
                        if(lastItem == totalItemCount) {




                        if (mListView.getCount() > 20) {

                            RelativeLayout footie = (RelativeLayout) getView().findViewById(R.id.footerview);

                            mListView.removeFooterView(footie);

                              }





                            else{

                                    updateData();
                                }

                            }

                        }

                    }

您只是在不擦除以前数据的情况下添加项目。