0

ListModelListを使用して選択肢のリストをZKリストボックスに追加しました。次に、これらの選択肢のリストをループして、必要な項目(「文字列」など)を見つけようとしました。このアイテム(「文字列」)を選択したアイテムとして設定する必要があります。

以下のコードを試しましたが、機能しません。これを行う方法はありますか?

  liveListModel = new ListModelList(new AppModelItem [] { 
        new AppModelItem("String", "string"), 
        new AppModelItem("Number", "number"), 
        new AppModelItem("Array", "array")
    });

    String choice [] = {"String", "Hello", "XYZ" };

    Listbox typesList = new Listbox();
    typesList.setModel(liveListModel);
    for (int i = 0; i < choice.length; i ++) {
        if (choice.[i] == typesList.getItemAtIndex(i).getValue().toString());
        typesList.setSelectedItem(typesList.getItemAtIndex(i));
    }

ありがとう、ソニー

4

1 に答える 1

1

このコードが元のコードであり、コピーしてエディターに貼り付けた場合は、if式の後のセミコロンを削除し、equals文字列が等しいかどうかをテストするために使用します。forループは次のようになります。

for (int i = 0; i < choice.length; i++) {
    if (choice[i].equals(typesList.getItemAtIndex(i).getValue().toString())) {
        typesList.setSelectedItem(typesList.getItemAtIndex(i));
    }
}

それでも機能しない場合は、デバッグコードを追加して、getValue()実際に正しい値が返されるかどうかを確認します。

for (int i = 0; i < choice.length; i++) {
    if (choice[i].equals(typesList.getItemAtIndex(i).getValue().toString())) {
        typesList.setSelectedItem(typesList.getItemAtIndex(i));
    } else {
      // DEBUG CODE
      System.out.printf("Expected: %s, found: %s%n", typesList.getItemAtIndex(i).getValue().toString());
}
于 2010-08-16T21:29:26.757 に答える