final List <CharSequence> checkedsurveylist = new ArrayList <CharSequence>();
final JSONArray jsonArray = new JSONArray(response);
final boolean[] checkedItems = new boolean[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = (JSONObject) jsonArray.get(i);
String syncstatus = "";
if (obj.get("syncstatus").toString().matches("1")) {
syncstatus = "Not yet synchronized";
checkedItems[i] = false;
} else if (obj.get("syncstatus").toString().matches("2")) {
syncstatus = "Synchronized";
checkedItems[i] = true;
}
checkedsurveylist.add("(" + obj.get("sysid").toString() + ")" + obj.get("surveytitle").toString() + " - " + syncstatus);
}
System.out.println("checkedItems " + checkedItems);
final List <String> mSelectedItems = new ArrayList <String>();
final List <Integer> mSelectedItemsindex = new ArrayList <Integer>();
AlertDialog.Builder builder = new AlertDialog.Builder(Connect_server.this);
builder.setTitle("Survey List")
.setMultiChoiceItems(checkedsurveylist.toArray(new CharSequence[checkedsurveylist.size()]), checkedItems,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which, boolean isChecked) {
checkedsurveylist.toArray()[which] = isChecked;
// Get the current focused item
String currentItem = checkedsurveylist.get(which).toString();
// Notify the current action
Toast.makeText(getApplicationContext(),
currentItem + " " + isChecked, Toast.LENGTH_SHORT).show();
if (isChecked) {
mSelectedItems.add(checkedsurveylist.get(which).toString());
mSelectedItemsindex.add(which);
} else if (mSelectedItems.contains(which)) {
mSelectedItemsindex.remove(Integer.valueOf(which));
mSelectedItems.remove(Integer
.valueOf(which));
}
}
});
// Set the positive/yes button click listener
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do something when click positive button
System.out.println("asdasdasd " + mSelectedItemsindex.size());
for (int i = 0; i < mSelectedItemsindex.size(); i++) {
System.out.println("asdasdasd " + checkedsurveylist.get(i).toString());
}
}
});
これは、アラートダイアログボックスでチェックボックスを使用してリストを作成するためのコードです。それは複数の選択を持つことができます。私の問題は、デフォルトチェックボックスの値を取得できることです。チェックボックスがユーザーによって手動でチェックされている場合にのみ値を取得できます。それはデフォルトでチェックすることである場合は、チェックボックスの値を取得する方法アラートダイアログのチェックボックスのデフォルト値を取得する方法
。 Update
ListView lv = ((AlertDialog) dialog).getListView();
System.out.println("asd" + "pos = " + lv.getCheckedItemPosition());
System.out.println("asd" + "which = " + which);
は、私はこれを使用しますが、[OK]ボタンをクリックするだけではなく、私は位置に-1を取得し、-1れているリスト項目とAlertDialog
で
CheckBoxのインスタンスで 'isChecked()'を呼び出しますか? – Blackbelt
@Blackbelt –
'for'ループの' ArrayList'sに事前チェック項目を追加するだけではどうですか? 'AlertDialog#getListView()。getCheckedItemPositions()'は、チェックされた位置で 'SparseBooleanArray'を返します。 –