Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-26104] Android: Picker inside Tableview resets value #10211

Merged
merged 20 commits into from
Oct 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,24 @@ public class PickerProxy extends TiViewProxy implements PickerColumnListener
private static final int MSG_SHOW_DATE_PICKER_DIALOG = MSG_FIRST_ID + 108;
private boolean useSpinner = false;
private boolean nativeSpinner = false;
private int lastSelectedIndex = -1;

public PickerProxy()
{
super();
defaultValues.put(TiC.PROPERTY_CALENDAR_VIEW_SHOWN, false);
}

public void setLastSelectedIndex(int index)
{
this.lastSelectedIndex = index;
}

public int getLastSelectedIndex()
{
return lastSelectedIndex;
}

@Override
public void handleCreationDict(KrollDict dict)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ protected void refreshNativeView()
// unnecessary event triggers.
spinner.setOnItemSelectedListener(null);
}
int rememberSelectedRow = getSelectedRowIndex(0);
int rememberSelectedRow = getPickerProxy().getLastSelectedIndex();
// Just one column - the first column - for now.
// Maybe someday we'll support multiple columns.
PickerColumnProxy column = getPickerProxy().getFirstColumn(false);
Expand Down Expand Up @@ -253,6 +253,10 @@ protected void refreshNativeView()
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long itemId)
{
// if the user selects a new item form the picker
// it should overwrite the values stored in preselected rows
getPickerProxy().getPreselectedRows().clear();
getPickerProxy().setLastSelectedIndex(position);
fireSelectionChange(0, position);

// Invalidate the parent view after the item is selected (TIMOB-13540).
Expand Down
53 changes: 53 additions & 0 deletions tests/Resources/ti.ui.picker.addontest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Appcelerator Titanium Mobile
* Copyright (c) 2011-Present by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
/* eslint-env mocha */
/* global Ti */
/* eslint no-unused-expressions: "off" */
'use strict';
var should = require('./utilities/assertions');

describe('Ti.UI.Picker', function () {

it.android('Selected index persistance', function (finish) {
var window = Ti.UI.createWindow();
// workaround iOS triggering of 'postlayout' event
var containerView = Ti.UI.createView();
var picker = Ti.UI.createPicker({});
var rows = [];
var indexToTest = 2;

for (var index = 0; index < 5; index++) {
rows.push(Ti.UI.createPickerRow({ title: 'Item ' + (index + 1).toString() }));
}

picker.add(rows);
window.add(picker);

picker.addEventListener('change', function () {
window.remove(picker);
containerView.addEventListener('postlayout', finishTest);
containerView.add(picker);
window.add(containerView);
});

picker.addEventListener('postlayout', changeItem);

window.open();

function changeItem() {
picker.removeEventListener('postlayout', changeItem);
picker.setSelectedRow(0, indexToTest);
}

function finishTest() {
if (rows.indexOf(picker.getSelectedRow(0)) === indexToTest) {
finish();
}
}
});

});