2011-07-04 108 views
4

我有我想要在列表視圖中顯示〜15,000行的數據庫表。我想顯示第一個100,當用戶滾動到最後一個項目時,下一個100應該加載(等等)。我已經在OnScrollListener()上實現,它調用AsyncTask負責加載更多項目。我得到的問題是我的SimpleCursorAdapter沒有更多的行添加到遊標後更新。我試過adapter.notifyDataSetChanged();但是這並沒有做任何事情。Android:無盡的滾動 - 列表視圖和光標

這是列表聽衆:

myListView.setOnScrollListener(new OnScrollListener(){ 
     public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 
      int lastInScreen = firstVisibleItem + visibleItemCount; 
      if(resultCursor != null){ 
       if(lastInScreen == totalItemCount && isLoadingMore == false){ 
        isLoadingMore = true; 
        loadedPage ++; 
        new LoadBooks().execute(); 
       } 
      } 
     } 
     public void onScrollStateChanged(AbsListView view, int scrollState) {} 
    }); 


這是我的AsyncTask類:

private class LoadBooks extends AsyncTask<String, Void, Void> { 
    private final ProgressDialog dialog = new ProgressDialog(FullIndex.this); 

    @Override 
    protected void onPreExecute() { 
     this.dialog.setMessage("Loading books..."); 
     this.dialog.show(); 
    } 

    @Override 
    protected Void doInBackground(String... arg0) { 
     try{ 
      resultCursor = dbHelper.fetchBooks(0, loadedPage * LIMIT_RESULTS); 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
     return null; 
    } 
    @Override 
    protected void onPostExecute(final Void unused){ 
     if(resultCursor != null){ 
      if(adapter == null){ 
       startManagingCursor(resultCursor); 
       String[] from = new String[]{"name"}; 
       int[] to = new int[]{R.id.book_item_tbx}; 
       getListView().setTranscriptMode(ListView.TRANSCRIPT_MODE_NORMAL); 
       adapter = new SimpleCursorAdapter(FullIndex.this, R.layout.book_item, resultCursor, from, to); 
       setListAdapter(adp); 
      }else{ 
       adapter.notifyDataSetChanged(); 
      } 
     } 
     if(dialog != null && dialog.isShowing()){ 
      dialog.dismiss(); 
     } 
     isLoadingMore = false; 
    } 
} 


新行添加到resultCursor,但沒有更新的列表中,我錯過了什麼?

回答

4

我會發佈一個代碼,我已經用它來填充每個滾動事件中的10條記錄。

/** 
    * Called when the activity is first created. 
    * 
    * @param savedInstanceState 
    *   the saved instance state 
    */ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.view_checkin_checkout_history); 

      Thread thread = new Thread() { 

      public void run() { 

         synchronized (this) { 
        fetchHistory(0); 

        handler.post(new Runnable() { 
         public void run() { 
          pd.dismiss(); 

          displayUI(); 
         }; 
        }); 
       } 
      } 
     }; 

     thread.start(); 
     } 

     /** 
    * Display the check in check out history list. 
    */ 
    private void displayUI() { 
     if ((checkInCheckOutHistoryList != null) 
       && (checkInCheckOutHistoryList.size() > 0)) { 
      historyArrayList = new ArrayList<HashMap<String, String>>(); 

      histroyListAdapter = new SimpleAdapter(
        ViewCheckInCheckOutHistory.this, historyArrayList, 
        R.layout.multi_colummn_list_text_style_small, new String[] { 
          "assetTag", "gif" , "action", "actionTime"}, 
        new int[] { R.id.list_content_column1, 
          R.id.list_content_imagecolumn, 
          R.id.list_content_column3, 
          R.id.list_content_column4}); 

      // To add more items to list view on scroll event. 
      historyListView.setOnScrollListener(new OnScrollListener() { 

       @Override 
       public void onScrollStateChanged(AbsListView view, 
         int scrollState) { 
       } 

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

        int lastInScreen = firstVisibleItem + visibleItemCount; 

        if ((lastInScreen == totalItemCount) && !(loadingMore) && (lastInScreen < totalHistoryItemCount)) { 

         if (!firstInstance) { 
          openSlider();       
         }       

         fetchHistory(lastInScreen);  

         Thread thread = new Thread(null, loadMoreListItems); 
         thread.start(); 
        } 
       } 
      }); 
        } 
        } 

// Runnable to load the items 

    private Runnable loadMoreListItems = new Runnable() { 

     @Override 
     synchronized public void run() { 
      // Set flag so we cant load new items 2 at the same time 
      loadingMore = true; 

      HashMap<String, String> historyObjectMap; 

      for (CheckInCheckOutHistory checkOutHistoryObj : checkInCheckOutHistoryList) { 
       historyObjectMap = new HashMap<String, String>(); 
       historyObjectMap.put("assetTag", 
         checkOutHistoryObj.getAssetTag()); 
       historyObjectMap.put("action", checkOutHistoryObj.getAction()); 
       historyObjectMap.put("actionTime", 
         checkOutHistoryObj.getActionDate()); 

       if (checkOutHistoryObj.getAction().equals("Checked out")) { 
        historyObjectMap.put("gif", R.drawable.radio_button_yellow 
          + ""); 
       } else { 
        historyObjectMap.put("gif", R.drawable.radio_button_green 
          + ""); 
       } 

       historyArrayList.add(historyObjectMap); 
      } 

      runOnUiThread(returnRes); 
     } 
    }; 

    // Since we cant update our UI from a thread this Runnable takes care of 
    // that! 
    private Runnable returnRes = new Runnable() { 
     @Override 
     public void run() { 

      // Add the new items to the adapter 
      if (historyArrayList != null && historyArrayList.size() > 0) { 
       histroyListAdapter.notifyDataSetChanged(); 
      } 

      if (firstInstance) { 
       historyListView.setAdapter(histroyListAdapter); 
       firstInstance = false; 
      } 

      historyListLayout.setVisibility(View.VISIBLE); 

      // Done loading more. 
      loadingMore = false; 

      if ((slidingDrawer.isOpened()) && (!loadingMore)) { 

       handler.postDelayed(new Runnable() { 

        @Override 
        public void run() { 
         slidingDrawer.close(); 
         slidingDrawer.setVisibility(View.GONE); 
        } 
       }, 1000); 

      } 
     } 
    }; 


fetchHistory(int count)是我用於爲totalHistoryItemCountcheckInCheckOutHistoryList &設置的值的方法。

希望這會有所幫助。