2016-11-25 67 views
0

我正在使用NativeScript Angular2並在我的頁面上添加了ListPicker供用戶選擇一個選項。下面是HTML文件中的代碼片段:NativeScript Angular2 - ListPicker無法在Android上工作

<ListPicker #languagePicker id="languagePicker" [visibility]="langSelectStatus()" [items]="languages" class="mek-select-field" (selectedIndexChange)="selectedIndexChanged(languagePicker)" 
> 
</ListPicker> 

以下是CSS:

.mek-select-field { 
    height: 70px; 
    border-color: lightblue; 
    border-width: 1px; 
    margin: 0; 
} 

我發現代碼運行而不會在iOS上的任何問題,以下是截圖: ListPicker on iOS

但是,在Android上,發現ListPicker無法正常工作。它顯示列表,但不能在定義的選項之間滾動。以下是截圖: ListPicker on Android

以下是環境信息:

  • NativeScript版本:2.4.0
  • NativeScript - 角版本:1.1.3
  • NativeScript Android的運行版本: 2.4.1
  • Android模擬器:API 25 Nexus 6

我是NativeScript的新手,不確定它是否與我的環境相關。

任何意見將非常受歡迎。 在此先感謝

[更新日期2016年11月26日]: 通過探索作爲建議由妮可和更多的測試樣品,我發現,當選項的值是從後端獲取通過HTTP服務的行爲纔會出現。例如,在本例中的創造,listpicker.component.ts類,如果我更改選項列表從http後臺檢索像下面的代碼:

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: ${list}`); 
       for (var i = 0; i < list.length; i++) { 
        this.pokemons.push(pokemonList[i]); 
       }   
      } 
     ); 

/*  for (var i = 0; i < pokemonList.length; i++) { 
      this.pokemons.push(pokemonList[i]); 
     }*/ 
    } 

    public selectedIndexChanged(picker) { 
     console.log('picker selection: ' + picker.selectedIndex); 
     this.picked = this.pokemons[picker.selectedIndex]; 
    } 
} 

凡具有完全相同的值作爲硬編碼的端點將響應值。當我在Android(模擬器和設備)上運行上面的代碼時,我發現ListPicker將不時顯示任何選項(或只有第一個選項)。重新制作非常容易。 iOS沒有這個問題。

我相信當ListPicker的選項來自Http後端時會存在一些問題,其中會出現一些延遲。

請諮詢 克拉倫斯

+0

發現了一些更多的信息。在ListPicker中,「items」用於Http服務API調用,我在ngOnInit()中調用http,然後在從後端返回結果時填充「languages」變量。它看起來像在Android上,當項目被填充後,ListPicker不會更新。在iOS中,這個問題沒有找到。 –

+0

Hi @Clarence,好的起點是在這裏查看這個示例項目 - https://github.com/NativeScript/nativescript-sdk-examples-ng,其中已經顯示,如何在NativeScript Angular 2項目中使用所有NativeScript組件。完全適合您的情況,您可以在這裏查看示例 - https://github.com/NativeScript/nativescript-sdk-examples-ng/tree/master/app/ui-category/listpicker/creating-listpicker。 –

+0

Hi @Nikolay,非常感謝您的回覆。我已經在示例代碼中進行了測試,並在我的帖子中添加了更多信息供您參考。非常感謝。 –

回答

0

爲了能夠您HTTP請求您應該接收到數據後創建新陣列,並讓舊陣列指向新的後面添加ListPicker項目。您可以查看下面附加的示例。

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> 

打字稿

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]; 
    } 
}