Android ListView选项保留

时间:2013-01-15 10:20:26

标签: android android-listview

我的活动是 singleTop

这是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;
    }

1 个答案:

答案 0 :(得分: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;
            }
        }