2015-03-03 69 views
0

我試圖追加更多的項目到Android項目的列表視圖。這是我的,我使用的代碼:Android的ListView追加更多的項目notifyDataSetChanged()不工作

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()追加項目的例子,但它似乎對我沒有任何影響。

+0

你沒有追加新的數據適配器,你只是創建新的數據新adpater。 – 2015-03-03 09:57:58

回答

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.ApplicationAdapter類創建addAllItems方法:

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

3.現在叫addAllItemsonFetchComplete方法的其他部分:

adapter.addAllItems(data); 
+0

絕對正確!非常感謝你 :) – jampez77 2015-03-03 22:02:32

相關問題