Merge "List.GetCheckItemIds no longer includes unchecked items."

This commit is contained in:
Gilles Debunne
2010-02-18 10:26:34 -08:00
committed by Android (Google) Code Review
2 changed files with 171 additions and 16 deletions

View File

@@ -16,14 +16,17 @@
package android.widget;
import com.android.internal.R;
import com.google.android.collect.Lists;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.PixelFormat;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
@@ -31,16 +34,13 @@ import android.util.SparseBooleanArray;
import android.view.FocusFinder;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SoundEffectConstants;
import android.view.View;
import android.view.ViewDebug;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.SoundEffectConstants;
import android.view.accessibility.AccessibilityEvent;
import com.google.android.collect.Lists;
import com.android.internal.R;
import java.util.ArrayList;
/*
@@ -2722,7 +2722,8 @@ public class ListView extends AbsListView {
/**
* Determine the distance to the nearest edge of a view in a particular
* direciton.
* direction.
*
* @param descendant A descendant of this list.
* @return The distance, or 0 if the nearest edge is already on screen.
*/
@@ -3307,9 +3308,9 @@ public class ListView extends AbsListView {
* Sets the checked state of the specified position. The is only valid if
* the choice mode has been set to {@link #CHOICE_MODE_SINGLE} or
* {@link #CHOICE_MODE_MULTIPLE}.
*
*
* @param position The item whose checked state is to be checked
* @param value The new checked sate for the item
* @param value The new checked state for the item
*/
public void setItemChecked(int position, boolean value) {
if (mChoiceMode == CHOICE_MODE_NONE) {
@@ -3392,10 +3393,11 @@ public class ListView extends AbsListView {
}
/**
* Returns the set of checked items ids. The result is only valid if
* the choice mode has not been set to {@link #CHOICE_MODE_SINGLE}.
*
* @return A new array which contains the id of each checked item in the list.
* Returns the set of checked items ids. The result is only valid if the
* choice mode has not been set to {@link #CHOICE_MODE_SINGLE}.
*
* @return A new array which contains the id of each checked item in the
* list.
*/
public long[] getCheckItemIds() {
if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null && mAdapter != null) {
@@ -3404,11 +3406,23 @@ public class ListView extends AbsListView {
final long[] ids = new long[count];
final ListAdapter adapter = mAdapter;
int checkedCount = 0;
for (int i = 0; i < count; i++) {
ids[i]= adapter.getItemId(states.keyAt(i));
if (states.valueAt(i)) {
ids[checkedCount++] = adapter.getItemId(states.keyAt(i));
}
}
return ids;
// Trim array if needed. mCheckStates may contain false values
// resulting in checkedCount being smaller than count.
if (checkedCount == count) {
return ids;
} else {
final long[] result = new long[checkedCount];
System.arraycopy(ids, 0, result, 0, checkedCount);
return result;
}
}
return new long[0];

View File

@@ -0,0 +1,141 @@
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.widget.listview;
import android.test.ActivityInstrumentationTestCase2;
import android.test.UiThreadTest;
import android.test.suitebuilder.annotation.MediumTest;
import android.widget.ListView;
import java.util.Arrays;
/**
* Testing the ListView getCheckItemIds() method in different situations.
*/
public class ListGetCheckItemIdsTest extends ActivityInstrumentationTestCase2<ListSimple> {
private ListView mListView;
public ListGetCheckItemIdsTest() {
super(ListSimple.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
mListView = getActivity().getListView();
}
private void assertChecked(String message, long... expectedIds) {
// Sort the two arrays since we are actually doing a set equality.
long[] checkItemIds = mListView.getCheckItemIds();
long[] sortedCheckItemsIds = new long[checkItemIds.length];
System.arraycopy(checkItemIds, 0, sortedCheckItemsIds, 0, checkItemIds.length);
Arrays.sort(sortedCheckItemsIds);
long[] sortedExpectedIds = new long[expectedIds.length];
System.arraycopy(expectedIds, 0, sortedExpectedIds, 0, expectedIds.length);
Arrays.sort(sortedExpectedIds);
assertTrue(message, Arrays.equals(sortedExpectedIds, sortedCheckItemsIds));
}
@MediumTest
@UiThreadTest
public void testNoneCheck() {
mListView.setChoiceMode(ListView.CHOICE_MODE_NONE);
mListView.setItemChecked(0, true);
assertChecked("None check choice has item checked");
}
@MediumTest
@UiThreadTest
public void testSimpleCheck() {
mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
assertChecked("Item checked when setting Single mode");
// Test a check at each position
int childCount = mListView.getChildCount();
for (int i=0; i<childCount; i++) {
mListView.setItemChecked(i, true);
assertChecked("Only element " + i + " should be checked", i);
}
// Check an element and uncheck some others
for (int i = 0; i < childCount; i++) {
mListView.setItemChecked(i, true);
mListView.setItemChecked((i - 3 + childCount) % childCount, false);
mListView.setItemChecked((i + 1) % childCount, false);
assertChecked("Only element " + i + " should be checked", i);
}
}
@MediumTest
@UiThreadTest
public void testMultipleCheck() {
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
assertChecked("Item checked when setting Multiple mode");
int childCount = mListView.getChildCount();
assertTrue("Tests requires at least 4 items", childCount >= 4);
mListView.setItemChecked(1, true);
assertChecked("First element non checked", 1);
mListView.setItemChecked(3, true);
assertChecked("Second element not checked", 1, 3);
mListView.setItemChecked(0, true);
assertChecked("Third element not checked", 0, 1, 3);
mListView.setItemChecked(2, false);
assertChecked("Unchecked element appears checked", 0, 1, 3);
mListView.setItemChecked(1, false);
assertChecked("Unchecked element remains", 0, 3);
mListView.setItemChecked(2, false);
assertChecked("Already unchecked element appears", 0, 3);
mListView.setItemChecked(3, false);
assertChecked("Unchecked 3 remains", 0);
mListView.setItemChecked(3, false);
assertChecked("Twice unchecked 3 remains", 0);
mListView.setItemChecked(0, false);
assertChecked("Checked items after last element unchecked");
}
@MediumTest
@UiThreadTest
public void testClearChoices() {
mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mListView.setItemChecked(0, true);
mListView.clearChoices();
assertChecked("Item checked after SINGLE clear choice");
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
int childCount = mListView.getChildCount();
for (int i = 0; i < childCount; i++) {
mListView.setItemChecked(0, i % 3 == 0);
}
mListView.clearChoices();
assertChecked("Item checked after MULTIPLE clear choice");
}
}