2017-02-03 4 views
2
Integer[] selsected; 
selected = new Integer[] {1,2}; 

これにより、固定サイズの2の整数配列が作成されます。Android:動的に初期化整数配列:マテリアルダイアログライブラリ

しかし、動的サイズのInteger[]アレイが必要です。 3つ以上の商品で時折...

私はMaterialDialog multichoice list dialogを使用しており、すでに選択されたアイテムを表示するためにInteger[]を使用しています。

私が選択したアレイの上に使用する場合は必ず選択して、それが1,2を示しており、ユーザーは23を選択した場合、私は、ユーザーが再びダイアログ選択したときに選択としてこれらの位置を示したいと思います。何か方法はありますか?

コード:

が初期化

Integer[] selected = new Integer[socalNetowrks.length]; 

答えて

1
あなたは配列リストと整数配列を使用することができます

List<Integer> myin=new ArrayList<>(); //you can add to this dynamically 

myin.add(1); //adding values 

myin.add(2); 

Integer [] selected = myin.toArray(new Integer[myin.size()]); //converting to array 

のArrayList:あなたのInteger[]としてInitilize

Integer[] selsected; 
selected = new Integer[] {1,2}; 
new MaterialDialog.Builder(this).title(R.string.socialNetworks) 
    .items(socialNetworks) 
    .itemsCallbackMultiChoice(selected, (dialog, which, text) -> { 
     StringBuilder str = new StringBuilder(); 
     for (int i = 0; i < which.length; i++) { 
      if (i > 0) str.append('\n'); 
      str.append(which[i]); 
      str.append(": "); 
      str.append(text[i]); 
     } 
     showToast(str.toString()); 
     return true; // allow selection 
    }) 
    .onNeutral((dialog, which) -> dialog.clearSelectedIndices()) 
    .onPositive((dialog, which) -> dialog.dismiss()) 
    .alwaysCallMultiChoiceCallback() 
    .positiveText(R.string.md_choose_label) 
    .autoDismiss(false) 
    .neutralText(R.string.clear_selection) 
    .show(); 
0

通常の配列以外の値を動的に追加するために使用されます。