ListPicker on Android - options not display correctly for values fetching from Http service
Bug description:
When populating a ListPicker with option values from remote backend via Http service, found that on Android, the values is not able to display intermittently.
Environment information:
- Nativescript version: 2.4.0
- nativescript-angular version: 1.1.3
- Nativescript Android runtime version: 2.4.1
Steps to re-produce
- Clone the NativeScript NG examples repo
- Modify the class "nativescript-sdk-examples-ng/app/ui-category/listpicker/creating-listpicker/creating-listpicker.component.ts" so that the Pokemon list is fetch from a remote backend via http instead of the hard-coded list (the backend is returning the value exactly the same as the hard-coded value):
import { Component } from "@angular/core";
import { Http } from '@angular/http';
var pokemonList = ["Bulbasaur", "Parasect", "Venonat", "Venomoth", "Diglett",
"Dugtrio", "Meowth", "Persian", "Psyduck", "Arcanine", "Poliwrath", "Machoke"];
@Component({
selector: "creating-listpicker",
templateUrl: "ui-category/listpicker/creating-listpicker/creating-listpicker.component.html"
})
export class CreatingListPickerComponent {
public pokemons: Array<string>;
public picked: string;
constructor(private http: Http) {
this.pokemons = [];
this.http.get('http://192.168.31.120:3000/pokemons').subscribe(
res => {
let list = res.json();
console.log(`Pokemon list: ${JSON.stringify(list)}`);
for (let i = 0; i < list.length; i++) {
this.pokemons.push(list[i]);
}
}
);
}
public selectedIndexChanged(picker) {
console.log('picker selection: ' + picker.selectedIndex);
this.picked = this.pokemons[picker.selectedIndex];
}
}
- Run the sample on Android emulator
tns livesync ios --emulator --watch
- Navigate to "Create ListPicker" sample
- Result is that when the view is shown, sometimes the full list display, sometimes nothing display, sometimes only the first option displayed, see the following screenshots for details
- The above problem didn't found on iOS
Screenshot 1: options display successfully

Screenshot 2: options not display

Screenshot 3: only first option display

Hi @clarenceh,
Thank you for your interest in NativeScript Angular2.
I reviewed your problem and found that this is something related with the change detection in Angular 2. The appropriate way to add ListPicker items after HTTP request is to create new array after receiving the data and to make the old array to point to the new one. You could review the below-attached sample code:
HTML
<FlexboxLayout flexDirection="column" exampleTitle toggleNavButton>
<Label class="h3 p-15 text-left" text="Pick a pokemon" textWrap="true"></Label>
<!-- >> creating-listpicker-html -->
<ListPicker #picker id="pickerid" class="p-15"
[items]="pokemons"
[selectedIndex]="selectedIndex"
(selectedIndexChange)="selectedIndexChanged(picker)">
</ListPicker>
<!-- << creating-listpicker-html -->
<Label [text]="'Selected pokemon: ' + picked" class="p-15" textWrap="true"></Label>
</FlexboxLayout>
TypeScript
import { Component , NgZone} from "@angular/core";
import { getFile, getImage, getJSON, getString, request } from "http";
var pokemonList = ["Bulbasaur", "Parasect", "Venonat", "Venomoth", "Diglett",
"Dugtrio", "Meowth", "Persian", "Psyduck", "Arcanine", "Poliwrath", "Machoke"];
@Component({
selector: "creating-listpicker",
templateUrl: "ui-category/listpicker/creating-listpicker/creating-listpicker.component.html"
})
export class CreatingListPickerComponent {
public pokemons: Array<string>;
public picked: string;
constructor(private zone:NgZone) {
this.pokemons = [];
}
ngOnInit(){
var that =this;
getJSON("https://httpbin.org/get?item1=item1&item2=item2&item3=item3")
.then((r) => {
console.log((<any>r).args.item1);
let args = (<any>r).args;
var arr = [args.item1, args.item2, args.item3]
this.pokemons = arr;
}, (e) => {
alert("GetJSON: " + e)
});
}
public selectedIndexChanged(picker) {
console.log('picker selection: ' + picker.selectedIndex);
this.picked = this.pokemons[picker.selectedIndex];
}
}
Hope this helps.
Hi @tsonevn,
Thanks a lot for the advice and this really helps.
Just wonder why in iOS this problem seems doesn't exist.
Best regards Clarence
Hi @clarenceh,
In my sample project, I was able to reproduce the same behavior on iOS as well as on Android.
Regards, @tsonevn
I opened another issue, but I haven't got a response yet, but working with ListPicker. Is there anyway to style this element. I have been stuck with this for quite a while now. Any help will be appreciated. #549