简体   繁体   English

如何使用BaseAdapter刷新ListView

[英]How can I refresh my ListView using BaseAdapter

I tried refresh my listview using notifyDataSetChanged() but this is marked on red. 我尝试使用notifyDataSetChanged()刷新列表视图,但这被标记为红色。 I use ListView like extended Adapter so notifyDataSetChaned it should work but is otherwise. 我使用ListView像扩展适配器一样,所以notifyDataSetChaned它应该可以工作,但是可以。

Below is my declaration ListView and ArrayList. 下面是我的声明ListView和ArrayList。

ListView listview;
ArrayList<ListData> myList = new ArrayList<>();

I set up my list in this way: 我以这种方式设置清单:

listview = (ListView) findViewById(R.id.listView);
listview.setAdapter(new MyBaseAdapter(this, myList));

I use also setOnScrollListener and I want that my listview it refresh when SROLL_STATE = 0; 我也使用setOnScrollListener,并且希望SROLL_STATE = 0时刷新列表视图。

    listview.setOnScrollListener(new AbsListView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            int position = listview.getFirstVisiblePosition();

            SCROLL_STATE = position;

            if (SCROLL_STATE == 0) {
                listview.notifyDataSetChanged();  // Here is a problem, because it's not work
            }

        }

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

        }
    });

notifyDataSetChanged() is a method of the BaseAdapter class and not the ListView class. notifyDataSetChanged()BaseAdapter类的方法,而不是 ListView类的方法。 Hence, it is logically and syntactically incorrect to call listView.notifyDatasSetChanged(); 因此,在逻辑上和语法上调用listView.notifyDatasSetChanged();是不正确的listView.notifyDatasSetChanged(); .

Replace this line with 将此行替换为

listView.getAdapter().notifyDataSetChanged();

All the best :) 祝一切顺利 :)

Define a Adapter object, MyBaseAdapter adapter=new MyBaseAdapter(this, myList); 定义一个适配器对象,MyBaseAdapter adapter = new MyBaseAdapter(this,myList); adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged();

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

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