mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Fixed #222: ListPicker showing number of items in list
This commit is contained in:
@@ -3,6 +3,7 @@ import helper = require("../helper");
|
|||||||
import viewModule = require("ui/core/view");
|
import viewModule = require("ui/core/view");
|
||||||
import listPickerTestsNative = require("./list-picker-tests-native");
|
import listPickerTestsNative = require("./list-picker-tests-native");
|
||||||
import pageModule = require("ui/page");
|
import pageModule = require("ui/page");
|
||||||
|
import application = require("application");
|
||||||
|
|
||||||
// <snippet module="ui/list-picker" title="ListPicker">
|
// <snippet module="ui/list-picker" title="ListPicker">
|
||||||
// # ListPicker
|
// # ListPicker
|
||||||
@@ -196,4 +197,60 @@ export var testWhenSelectingAnItemNativelySelectedIndexIsUpdatedProperly = funct
|
|||||||
finally {
|
finally {
|
||||||
helper.goBack();
|
helper.goBack();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export var test_Android_MaxValueIsOneLessThanItemsCount = function () {
|
||||||
|
if (!application.android) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
helper.buildUIAndRunTest(_createListPicker(), function (views: Array<viewModule.View>) {
|
||||||
|
var listPicker = <listPickerModule.ListPicker>views[0];
|
||||||
|
listPicker.items = ["One", "Two", "Three"];
|
||||||
|
var expectedValue = listPicker.items.length - 1;
|
||||||
|
var actualValue = (<any>listPicker).android.getMaxValue();
|
||||||
|
TKUnit.assert(actualValue === expectedValue, "Actual: " + actualValue + "; Expected: " + expectedValue);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export var test_Android_WhenItemsAreEmptyNativeControlDoesNotShowZero = function () {
|
||||||
|
if (!application.android) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
helper.buildUIAndRunTest(_createListPicker(), function (views: Array<viewModule.View>) {
|
||||||
|
var listPicker = <listPickerModule.ListPicker>views[0];
|
||||||
|
var expectedValue = "";
|
||||||
|
var actualValue = (<any>listPicker)._editText.getText().toString();
|
||||||
|
TKUnit.assert(actualValue === expectedValue, "Actual: " + actualValue + "; Expected: " + expectedValue);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export var test_Android_WhenBoundToSingleElementArrayEditTextIsUpdatedProperly = function () {
|
||||||
|
if (!application.android) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
helper.buildUIAndRunTest(_createListPicker(), function (views: Array<viewModule.View>) {
|
||||||
|
var listPicker = <listPickerModule.ListPicker>views[0];
|
||||||
|
listPicker.items = ["One"];
|
||||||
|
var expectedValue = "One";
|
||||||
|
var actualValue = (<any>listPicker)._editText.getText().toString();
|
||||||
|
TKUnit.assert(actualValue === expectedValue, "Actual: " + actualValue + "; Expected: " + expectedValue);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export var test_Android_WhenSelectedIndexChangesEditTextIsUpdatedProperly = function () {
|
||||||
|
if (!application.android) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
helper.buildUIAndRunTest(_createListPicker(), function (views: Array<viewModule.View>) {
|
||||||
|
var listPicker = <listPickerModule.ListPicker>views[0];
|
||||||
|
listPicker.items = ["One", "Two"];
|
||||||
|
listPicker.selectedIndex = 1;
|
||||||
|
var expectedValue = "Two";
|
||||||
|
var actualValue = (<any>listPicker)._editText.getText().toString();
|
||||||
|
TKUnit.assert(actualValue === expectedValue, "Actual: " + actualValue + "; Expected: " + expectedValue);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
@@ -10,6 +10,7 @@ export class ListPicker extends common.ListPicker {
|
|||||||
private _android: android.widget.NumberPicker;
|
private _android: android.widget.NumberPicker;
|
||||||
private _valueChangedListener: android.widget.NumberPicker.OnValueChangeListener;
|
private _valueChangedListener: android.widget.NumberPicker.OnValueChangeListener;
|
||||||
private _formatter: android.widget.NumberPicker.Formatter;
|
private _formatter: android.widget.NumberPicker.Formatter;
|
||||||
|
private _editText: android.widget.EditText;
|
||||||
|
|
||||||
get android(): android.widget.NumberPicker {
|
get android(): android.widget.NumberPicker {
|
||||||
return this._android;
|
return this._android;
|
||||||
@@ -21,7 +22,6 @@ export class ListPicker extends common.ListPicker {
|
|||||||
|
|
||||||
public _createUI() {
|
public _createUI() {
|
||||||
this._android = new android.widget.NumberPicker(this._context);
|
this._android = new android.widget.NumberPicker(this._context);
|
||||||
this._android.setMinValue(0);
|
|
||||||
this._android.setDescendantFocusability(android.widget.NumberPicker.FOCUS_BLOCK_DESCENDANTS);
|
this._android.setDescendantFocusability(android.widget.NumberPicker.FOCUS_BLOCK_DESCENDANTS);
|
||||||
|
|
||||||
var that = new WeakRef(this);
|
var that = new WeakRef(this);
|
||||||
@@ -36,7 +36,7 @@ export class ListPicker extends common.ListPicker {
|
|||||||
return this.owner._getItemAsString(index);
|
return this.owner._getItemAsString(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
return index.toString();
|
return "";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
this._android.setFormatter(this._formatter);
|
this._android.setFormatter(this._formatter);
|
||||||
@@ -54,7 +54,15 @@ export class ListPicker extends common.ListPicker {
|
|||||||
});
|
});
|
||||||
this._android.setOnValueChangedListener(this._valueChangedListener);
|
this._android.setOnValueChangedListener(this._valueChangedListener);
|
||||||
|
|
||||||
this._fixDisappearingSelectedItem();
|
//Fix disappearing selected item.
|
||||||
|
//HACK: http://stackoverflow.com/questions/17708325/android-numberpicker-with-formatter-does-not-format-on-first-rendering/26797732
|
||||||
|
var mInputTextField = java.lang.Class.forName("android.widget.NumberPicker").getDeclaredField("mInputText");
|
||||||
|
mInputTextField.setAccessible(true);
|
||||||
|
this._editText = <android.widget.EditText>mInputTextField.get(this._android);
|
||||||
|
this._editText.setFilters([]);
|
||||||
|
|
||||||
|
// Since the Android NumberPicker has to always have at least one item, i.e. minValue=maxValue=value=0, we don't want this zero showing up when this.items is empty.
|
||||||
|
this._editText.setText(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public _onSelectedIndexPropertyChanged(data: dependencyObservable.PropertyChangeData) {
|
public _onSelectedIndexPropertyChanged(data: dependencyObservable.PropertyChangeData) {
|
||||||
@@ -77,7 +85,7 @@ export class ListPicker extends common.ListPicker {
|
|||||||
maxValue = 0;
|
maxValue = 0;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
maxValue = data.newValue.length;
|
maxValue = data.newValue.length - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.android.setMaxValue(maxValue);
|
this.android.setMaxValue(maxValue);
|
||||||
@@ -85,13 +93,11 @@ export class ListPicker extends common.ListPicker {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this._updateSelectedIndexOnItemsPropertyChanged(data.newValue);
|
this._updateSelectedIndexOnItemsPropertyChanged(data.newValue);
|
||||||
}
|
|
||||||
|
//Fix disappearing selected item.
|
||||||
private _fixDisappearingSelectedItem() {
|
|
||||||
//HACK: http://stackoverflow.com/questions/17708325/android-numberpicker-with-formatter-does-not-format-on-first-rendering/26797732
|
//HACK: http://stackoverflow.com/questions/17708325/android-numberpicker-with-formatter-does-not-format-on-first-rendering/26797732
|
||||||
var mInputTextField = java.lang.Class.forName("android.widget.NumberPicker").getDeclaredField("mInputText");
|
if (this._editText) {
|
||||||
mInputTextField.setAccessible(true);
|
this._editText.setFilters([]);
|
||||||
var mInputText = <android.widget.EditText>mInputTextField.get(this._android);
|
}
|
||||||
mInputText.setFilters([]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user