[英]Android : Dynamically Initialize Integer Array : Material Dialog Library
Integer[] selsected;
selected = new Integer[] {1,2};
這將生成一個固定大小為 2 的整數數組。
但我需要一個動態大小的Integer[]
數組。 有時有 3 個或更多項目…
因為我使用的是MaterialDialog 多選列表對話框,它使用Integer[]
來顯示已選擇的項目。
如果我使用上面選擇的數組,它總是顯示1,2
為選中狀態,如果用戶選擇了2
和3
我想在用戶再次選擇對話框時將這些位置顯示為選中狀態。 有什么辦法嗎?
代碼:
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();
您可以使用數組列表和整數數組
初始化
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[]
初始化為:
Integer[] selected = new Integer[socalNetowrks.length];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.