2013-01-15 59 views
-1

我有一個活性singleTop的Android ListView中選擇將被保留

這裏是OnCreate中()中的部分

ListView mainListView = (ListView) findViewById(R.id.order_item_list); 
    mainListView.clearChoices(); 
    mainListView.requestLayout(); 

    listAdapter = new OrderItemAdapter(this, 
      R.layout.list_item_order_item, OrderActivity.Order.OrderItems); 

    mainListView.setAdapter(listAdapter); 

    SparseBooleanArray checkedItems= mainListView.getCheckedItemPositions(); 
    Log.println(2, "s", Integer.toString(checkedItems.size())); 
} 

使用debuger checkedItems是0,那麼。但是,當我從行動中得到它時,點擊checkedItems具有以前的值。再次使用調試器的引用似乎是在同一個對象。是否有什麼我可以做這件事?因爲它可能會導致很多問題。

@Override 
    public boolean onOptionsItemSelected(MenuItem menuItem) { 
     switch (menuItem.getItemId()) { 
     case android.R.id.home: 

      //finish(); 
      // NavUtils.navigateUpFromSameTask(this); 
      confirmExit(); 
      return true; 

     case R.id.delete_button: 
      ListView mainListView = (ListView) findViewById(R.id.order_item_list); 

      SparseBooleanArray checkedItems= mainListView.getCheckedItemPositions(); 
      boolean hasOld=false; 
      break; 
     } 
     return true; 
    } 

回答

0

問題似乎來自這裏。

https://github.com/android/platform_frameworks_base/blob/android-cts-2.3_r12/core/java/android/widget/ListView.java

@Override 
     public Parcelable onSaveInstanceState() { 
      Parcelable superState = super.onSaveInstanceState(); 
      return new SavedState(superState, mCheckStates, mCheckedIdStates); 
     } 

     @Override 
     public void onRestoreInstanceState(Parcelable state) { 
      SavedState ss = (SavedState) state; 

      super.onRestoreInstanceState(ss.getSuperState()); 

      if (ss.checkState != null) { 
       mCheckStates = ss.checkState; 
      } 

      if (ss.checkIdState != null) { 
       mCheckedIdStates = ss.checkIdState; 
      } 
     }