Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-26104] Android: Removing and re-adding drop-down picker resets last selection

GitHub Issuen/a
TypeBug
PriorityCritical
StatusClosed
ResolutionFixed
Resolution Date2018-10-02T17:54:17.000+0000
Affected Version/sRelease 6.2.0
Fix Version/sRelease 7.5.0
ComponentsAndroid
Labelsandroid
ReporterAlexander de Greiff
AssigneeYordan Banev
Created2018-06-04T17:10:17.000+0000
Updated2018-10-02T17:54:34.000+0000

Description

Starting with Ti 7.x incl. 7.1.1GA on Android the pickerview, when inside a tableview, resets its selected value after events like orientationchange, scroll outside of window and back in or focus on another ui element. (works perfectly on ios and used to work perfectly on Ti<=6.x) example code: {{ var currentwindow = Ti.UI.createWindow({ }); var tabledata = []; var picker = Ti.UI.createPicker({ top:20 }); var data = []; data[0]=Ti.UI.createPickerRow({title:'Bananas'}); data[1]=Ti.UI.createPickerRow({title:'Strawberries'}); data[2]=Ti.UI.createPickerRow({title:'Mangos'}); picker.add(data); var myrow=Ti.UI.createTableViewRow({ 'height' : '50sp' }); myrow.add(picker); tabledata.push(myrow); var infotableview = Titanium.UI.createTableView({ data:tabledata, }); currentwindow.add(infotableview); //currentwindow.add(picker); }}

Comments

  1. Alexander de Greiff 2018-06-17

    what will happen with this issue now? do you need more info?
  2. Michael Gangolf 2018-06-25

    I couldn't locate the problem but these _could_ be related to the bug: When e.g. the orientation changes the picker is created again and it is losing its [old selection value (TiUINativePicker.java)](https://github.com/appcelerator/titanium_mobile/blob/cd91879899ff3629ae78b369e1aa67ca57f48999/android/modules/ui/src/java/ti/modules/titanium/ui/widget/picker/TiUINativePicker.java#L215). These PRs did some changes https://github.com/appcelerator/titanium_mobile/pull/9763/files https://github.com/appcelerator/titanium_mobile/pull/9917/files https://github.com/appcelerator/titanium_mobile/pull/9963/files so that would be a good place to start some debugging. If I have some more spare time I might have a look again, too
  3. Joshua Quick 2018-07-10

    Okay. So, I've confirmed that this is not just an issue with a TableView. The root issue here is that if you remove the picker from a window and then re-add it, the picker will forget its last selected item and reset to the 1st item. The reason this can happen with a TabelView too is because Android's native behavior is to remove views that are scrolled off (for performance reasons) and then re-add them when they're scrolled-in. (I'm curious if this would be an issue with TabGroups as well.) Here's how to reproduce this issue without a TableView... *Steps to reproduce:*

    Build and run the below code on Android.

    Tap on the picker.

    Select a picker item other than "Item 1".

    Tap on the "Remove Picker" button. (This removes it from the window.)

    Tap on the "Add Picker" button. (This re-adds it to the window.)

    Notice that the picker's selection has been reset to "Item 1".

       var window = Ti.UI.createWindow();
       var picker = Ti.UI.createPicker({ width: "50%" });
       var rows = [];
       for (var index = 0; index < 20; index++) {
       	rows.push(Ti.UI.createPickerRow({ title: "Item " + (index + 1).toString() }));
       }
       picker.add(rows);
       window.add(picker);
       function createButtonText() {
       	return window.children.includes(picker) ? "Remove Picker" : "Add Picker";
       }
       var button = Ti.UI.createButton({
       	title: createButtonText(),
       	bottom: "20dp",
       });
       button.addEventListener("click", function(e) {
       	if (window.children.includes(picker)) {
       		window.remove(picker);
       	} else {
       		window.add(picker);
       	}
       	button.title = createButtonText();
       });
       window.add(button);
       window.open();
       
    [~ybanev], this is an issue with the picker, not the table view. I think the solution may be to override the picker's onAttachedToWindow() method and restore the selection there. It's worth a shot.
  4. Joshua Quick 2018-07-10

    [~adegreiff], is there any reason you're using a TableView instead of a ScrollView? I ask because a ScrollView is significantly simpler and would not have this picker issue.
  5. Alexander de Greiff 2018-07-10

    [~jquick] , we have some enterprise client apps in production where the tableview is the central scaffold and the rows and containing ui objects/properties are coming from the server and interpreted/cached in the client (titanium classic). we are evaluating listviews but they are not as flexible as the layout can change with every row coming from the server.
  6. Joshua Quick 2018-07-10

    I've tested for this issue in Titanium 6.0.2 and have confirmed that the picker "forgets" its last selection when detached/re-attached to the window as well. This looks like a Google issue/limitation with the picker that has always existed that we need to work-around. Perhaps Titanium's TableView has work-around it at one point, but regardless, this issue needs to be resolved in the picker's Java code. We'll look into it. Thanks for reporting this issue.
  7. Yordan Banev 2018-07-25

    PR: https://github.com/appcelerator/titanium_mobile/pull/10211
  8. Keerthi Mahalingam 2018-09-18

    FR passed.
  9. Christopher Williams 2018-10-02

    Closing since FR passed as well.

JSON Source