有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何检查ListView在数据更新后是否更改

我有一个未解决的问题,我无法回答自己。有没有办法,如何检查ListView(ListView.getCount();)在更新数据后是否发生了更改?更准确地说,是在加载更多进程后。我想删除页脚视图(ProgressBar),因为在更新方法之后,列表视图保持不变

有办法吗

如有必要,检查下面的短代码

提前谢谢

我的方法增加了更多数据:

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);

    Log.v("updateData", "uploading data from Parse.com");
    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();

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

                    mAdapter.add(animals.get(i));

                }  

            }  
        }
    }); 
}

我的OnScrollListener发现数据必须更新:

mListView.setOnScrollListener(new OnScrollListener() {

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                         int totalItemCount) {
    }


    @Override
    public void onScrollStateChanged(AbsListView view,
        int scrollState) {

        int threshold = 1;
        int count = mListView.getCount();

        if (scrollState == SCROLL_STATE_IDLE) {
            if (mListView.getLastVisiblePosition() >= count - threshold){

                updateData();
                //here do I need to add - if ListView didnt change - > remove footer                        
            }

        }
        if (SCROLL_STATE_TOUCH_SCROLL == scrollState) {
            View currentFocus = getActivity().getCurrentFocus();

            if(currentFocus != null) {
                currentFocus.clearFocus();
            }
        }
    } 
});

共 (1) 个答案

  1. # 1 楼答案

    您可以扩展适配器并单独设置数据

    public void updateData() {
        ...
    
        query.findInBackground(new FindCallback<Animal>() {
            @Override
            public void done(List<Animal> animals, ParseException error) {
    
                if(animals != null){
                    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.setItems(animals);
                }  
            }
        }); 
    }
    
    class CustomAdapter extends ArrayAdapter {
        public void setItems(List<Animal> animals) {
            int oldCount = getCount();
            int newCount = animals.size();
            clear();
            addAll(animals);
            if (oldCount != newCount) {
                // Do whatever you want here. The number of items changed.
            }
        }
    }