2013-07-26 61 views
1

我有一個數據庫,裏面有大約12000條記錄,我想在ListView中加載。我使用BaseAdapter。當我在ListView中加載項目時,它需要很長時間。
有沒有什麼辦法可以減少這個時間,例如我看到一個只加載有限數量的項目的應用程序,直到滾動條到達ListView的末尾,然後再次加載更多的項目。在listview中加載大型數據庫

+0

集從查詢像你說的限制 http://stackoverflow.com/questions/1407442/android-sqlite-and-huge-data-sets – tyczj

回答

0

您應該設置onScrollListener在你的清單,跟蹤項目的可見性和偏移量。我告訴你這裏一個例子:

// Adapter for the custom list 
adapter = new Adapter(this, activityList); 
setListAdapter(adapter); 
registerForContextMenu(getListView()); 
getListView().setOnScrollListener(new OnScrollListener(){ 
    public void onScroll(AbsListView lw, final int firstVisibleItem, 
    final int visibleItemCount, final int totalItemCount) { 

     switch(lw.getId()) { 
      case android.R.id.list:  

       // Make your calculation stuff here. You have all your 
       // needed info from the parameters of this function. 

       // Sample calculation to determine if the last 
       // item is fully visible. 
       final int lastItem = firstVisibleItem + visibleItemCount; 
       if(lastItem == totalItemCount) { 
        // Last item is fully visible. 
        Log.i("a", "last item fully visible..."); 

        try { 
         if(offset > 0){ 
          int newLimit; 
          int oldOffset = offset; 
          if(offset >= limit){ 
          newLimit = limit; 
          offset = offset - limit; 
         } 
         else{ 
          newLimit = length; 
          offset = 0; 
         } 
         for (int i=0; i < newLimit; i++) 
         { 
          JSONObject item = jFeed.getJSONObject(i + length - oldOffset); 
          // Pulling items from the array 

          // Get list info 
          String sInfo = item.getString(TAG_INFO); 
          Log.i(MainActivity.class.getName(), "Info: " + sInfo); 

          // Populate the dynamic custom list 
          HashMap<String, String> map = new HashMap<String, String>(); 
          map.put(KEY_INFO, sInfo); 

          activityList.add(map); 
         } 
         adapter.notifyDataSetChanged(); 

        } catch (JSONException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 
     } 
    } 

    public void onScrollStateChanged(AbsListView view, int scrollState) { 
     // TODO Auto-generated method stub 
     if(scrollState == 0) 
      Log.i("a", "scrolling stopped..."); 
    } 
});