私はAndroidアプリでリストを持つAlertDialogを構築しようとしています。リストを使って警告ダイアログを作成する
AlertDialog.Builder builder = new AlertDialog.Builder(HomeActivity.this);
builder.setTitle("Pick a branch to navigate to:");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1);
adapter.addAll(branches);
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent locationIntent = new Intent(getApplicationContext(), NavigationActivity.class);
locationIntent.putExtra("branch", which);
startActivity(locationIntent);
}
});
builder.show();
を物事を明確にする:
これは私がダイアログを構築するために使用しているコードであるbranches
はSet<String>
です。 intented活動からのログで
、私が言ってメッセージを取得:
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
私はコマンドの上記の例外は、私はAlertDialogが空である理由だと思いれるString[]
に変換Set<String>
を使用して取得します。
私はそれを解決し、リストにSet<String>
の項目が含まれているAlertDialogを表示できますか?
あなたのセットは空です。 設定を初期化して項目を追加するコードを表示してください –
ブランチのセットがアダプタに追加される前に空でないことを確認してください! –
@VishalChhodwani、同じコードで別のセットと同じブロックで初期化し、もう1つがうまく動作するので、セットは空ではありません。 – Ido