简体   繁体   English

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

[英]How to check, if the ListView changed after data update

I have one unsolved question, which I couldn't accomplish to answer myself. 我有一个未解决的问题,我无法回答自己。 Is there any way, how to check, if the ListView ( ListView.getCount(); ) changed after updating the data - more accurately, after load-more process. 有什么方法,如何检查在更新数据后ListView( ListView.getCount(); )是否发生了更改-更准确地说,是在进行更多加载之后。 I want to remove the footer view ( ProgressBar ) after the ListView stays the same after my update method. 我想在更新方法后ListView保持不变后删除页脚视图( ProgressBar )。

Is there any way how to do it? 有什么办法吗?

If necessary, check short codes below 如有必要,请检查下面的短代码

Thanks in advance! 提前致谢!

My method, which adds more data: 我的方法,它添加了更多数据:

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

                }  

            }  
        }
    }); 
}

My OnScrollListener, which finds out, that the data have to be updated: 我的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();
            }
        }
    } 
});

You can extend the adapter and set the data separately. 您可以扩展适配器并分别设置数据。

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.
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM