2011-05-20 106 views
1

我有一個多列listView,我想動態地添加10個項目到滾動事件的列表中。動態添加點亮的項目已經成功,但是當添加接下來的10個項目時,列表視圖選擇會回到列表的第一個項目。動態添加項目到多列listView滾動

這是我的代碼:

public class ViewCheckInCheckOutHistory extends Activity { 
     ListView historyListView; 

     ProgressDialog pd; 
     List<CheckInCheckOutHistory> checkInCheckOutHistoryList = new ArrayList<CheckInCheckOutHistory>();  

     ArrayList<HashMap<String, String>> historyArrayList; 
     SimpleAdapter histroyListAdapter; 

    int itemsPerPage = 10; 
     boolean loadingMore = false; 

     int firstItemCount = 0; 
     int lastItemCount = 10; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.view_checkin_checkout_history);  

     pd = ProgressDialog.show(this, "", "Please wait..."); 

     Thread thread = new Thread() { 

      public void run() { 

      synchronized (this) { 
       fetchHistory(); 

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

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

     thread.start(); 
     } 

    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", "action", "actionTime" }, new int[] { 
      R.id.list_content_column1, 
      R.id.list_content_column3, 
      R.id.list_content_column4}); 

     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)){     
       Thread thread = new Thread(null, loadMoreListItems); 
       thread.start(); 
      }    
      } 
     }); 
     } 
     } 


     //Runnable to load the items 
     private Runnable loadMoreListItems = new Runnable() { 

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

      HashMap<String, String> historyObjectMap; 

      System.out.println("firstItemCount : "+firstItemCount); 
      System.out.println("lastItemCount : "+lastItemCount); 

      //Get 10 new listitems 
      for (int i = firstItemCount; i < lastItemCount; i++) { 
      System.out.println("i : "+i); 
      CheckInCheckOutHistory checkOutHistoryObj = checkInCheckOutHistoryList.get(i); 

      historyObjectMap = new HashMap<String, String>(); 
      historyObjectMap.put("assetTag", checkOutHistoryObj.getAssetTag()); 
      historyObjectMap.put("action", checkOutHistoryObj.getAction()); 

      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 (checkInCheckOutHistoryList.size() - lastItemCount > 10) { 
      firstItemCount = lastItemCount; 
      lastItemCount = lastItemCount + itemsPerPage; 
      } else { 
      if (checkInCheckOutHistoryList.size() - firstItemCount > 10) { 
       firstItemCount = lastItemCount; 
       lastItemCount = checkInCheckOutHistoryList.size(); 
      } else { 
       if (checkInCheckOutHistoryList.size() - firstItemCount > 0){ 
       firstItemCount = lastItemCount; 
       lastItemCount = checkInCheckOutHistoryList.size(); 
       } 
      } 
      } 

      historyListView.setAdapter(histroyListAdapter); 

      //Done loading more. 
      loadingMore = false; 
     } 
     }; 
    } 

'fetchHistory()' 是其中值是在 'checkInCheckOutHistoryList' 添加的功能。

在此先感謝您的幫助。

+0

單從第一項顯示出來,因爲你指定的適配器連接到'ListView' 。對'notifyDatasetChanged()'的調用應該足夠了。另一方面,使用'AsyncTask'會比'Runnables'顯式線程更好。 – rekaszeru 2011-05-20 05:59:59

回答

3

displayUI()方法,每次要創建一個新的適配器,並將其設置到ListView一遍又一遍,而不是使用Adapter.notifyDataSetChanged()

+0

我嘗試通過刪除historyListView.setAdapter(histroyListAdapter);來自Runnable returnRes。但它沒有奏效。 – Ian 2011-05-20 09:46:46

+0

你能告訴我在哪裏可以將適配器設置爲listView? – Ian 2011-05-20 09:57:20

+0

我告訴你在displayUI方法 – ingsaurabh 2011-05-20 10:19:47