Android ListView追加更多项目notifyDataSetChanged()不起作用

时间:2015-03-03 09:53:42

标签: android listview scroll notifydatasetchanged

我正在尝试将更多项目附加到Android项目的列表视图中。这是我正在使用的代码:

public class NewsFeedActivity extends ListActivity implements FetchDataListener{

boolean loadingMore = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_news_feed);

    SharedPreferences shared = getSharedPreferences(MainActivity.class.getSimpleName(),Context.MODE_PRIVATE);
    @SuppressWarnings("unused")
    String storedUID = (shared.getString(UID, ""));

    final ListView list = (ListView)findViewById(android.R.id.list);
    swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
    swipeLayout.setColorSchemeResources(R.color.black, R.color.white, R.color.black, R.color.white);
    swipeLayout.setOnRefreshListener(new OnRefreshListener() {

        @Override
        public void onRefresh() {
             new Handler().postDelayed(new Runnable() {
                    @Override public void run() {
                        swipeLayout.setRefreshing(false);
                        loadingMore = true;
                        initView();
                    }
                }, 1000);

        }

    });

    list.setOnScrollListener(new OnScrollListener(){

        private int currentFirstVisibleItem;
        private int currentVisibleItemCount;
        private int totalItem;

        @Override
           public void onScrollStateChanged(AbsListView view, int scrollState) {
               this.isScrollCompleted();  
           }

           private void isScrollCompleted() {
            // TODO Auto-generated method stub
               int lastInScreen = currentFirstVisibleItem + currentVisibleItemCount;    
               if((lastInScreen == totalItem) && !(loadingMore)){     
                   //Toast.makeText(getApplicationContext(), "Loading more...", Toast.LENGTH_LONG).show();
                   loadingMore = true;
                   initView();


               }
           }



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

            this.currentFirstVisibleItem = firstVisibleItem;
            this.currentVisibleItemCount = visibleItemCount;
            this.totalItem = totalItemCount;
        }

          });


    initView();

}

以下是我用来调用列表的其他两个函数:

private void initView() {
    // show progress dialog
    if(!loadingMore == true){
    dialog = ProgressDialog.show(this, "", "Loading...");
    }
    String url = SERVER_URL+getBlackCards;
    FetchDataTask task = new FetchDataTask(this);
    task.execute(url);
}

public void onFetchComplete(List<Application> data) {
    // dismiss the progress dialog

    if(dialog != null)  dialog.dismiss();
    // create new adapter
    ApplicationAdapter adapter = new ApplicationAdapter(this, data);
    // set the adapter to list

    setListAdapter(adapter);  

    if(loadingMore == true){
        adapter.notifyDataSetChanged();
    }

    loadingMore = false;
}

当前行为基本上只是一个刷新,其中所有项目都被替换,我滚动回到列表的顶部。在做了一些环顾之后,我发现了很多使用notifyDataSetChanged()来附加项目的例子,但它似乎对我没有任何影响。

1 个答案:

答案 0 :(得分:1)

  

当前行为基本上只是刷新所有项目   被替换,我被滚动回到列表的顶部

因为在这里:

 ApplicationAdapter adapter = new ApplicationAdapter(this, data);
 setListAdapter(adapter);  

每次创建adapter类的新对象,而不是在ListView的当前适配器中添加新项目。

这样做:

1。在课程级别创建ApplicationAdapter对象:

ApplicationAdapter adapter;
public void onFetchComplete(List<Application> data) {
    // dismiss the progress dialog

    if(dialog != null)  dialog.dismiss();
    // create new adapter
    if(adapter==null){
    adapter = new ApplicationAdapter(this, data);
    // set the adapter to list
    setListAdapter(adapter);  
    }else{
         // update current adapter
    }
    loadingMore = false;
}

2。addAllItems课程中创建ApplicationAdapter方法:

public void addAllItems(List<Application> data){
  this.data.addAll(data);
  this.notifyDataSetChanged();
}

3。现在在addAllItems方法的其他部分调用onFetchComplete

  adapter.addAllItems(data);