初めての Android アプリを開発しています。カスタムの複数選択でダイアログを表示したい。できますが、アイテムをクリックすると、builder.show() を再度呼び出さない限り、ダイアログが閉じられます。私のコードは次のとおりです。
case R.id.menu_references:
final boolean[] oldValue = checkedItems.clone();
final AlertDialog.Builder referenceBuilder = new AlertDialog.Builder(this);
referenceBuilder.setPositiveButton(context.getString(R.string.save), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
if (!Arrays.equals(checkedItems, oldValue)) {
saveSettings(oldValue, checkedItems);
}
}
});
final BaseAdapter adapter = new ReferencesAdapter(context, checkedItems);
referenceBuilder.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
checkedItems[which] = !checkedItems[which];
adapter.notifyDataSetChanged();
referenceBuilder.show(); //IF I REMOVE THIS LINE, THE DIALOG IS BEING CLOSED
}
});
referenceBuilder.setNegativeButton(context.getString(R.string.cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
checkedItems = oldValue;
dialog.dismiss();
}
});
referenceBuilder.create().show();
return true;
そしてアダプタクラス:
public class ReferencesAdapter extends BaseAdapter {
private LayoutInflater inflater;
private boolean checkedItems[];
private Context context;
private SettingOption[] values;
public ReferencesAdapter(Context context, boolean[] checkedItems) {
super();
this.checkedItems = checkedItems;
inflater = LayoutInflater.from(context);
this.values = SettingOption.values();
this.context = context;
}
@Override
public int getCount() {
return SettingOption.values().length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null) {
convertView = inflater.inflate(R.layout.activity_reference, null);
holder = new ViewHolder();
holder.check_title =(CheckedTextView)convertView.findViewById(R.id.setting_check);
holder.check_image =(ImageView)convertView.findViewById(R.id.setting_image);
convertView.setTag(holder);
} else {
holder =(ViewHolder)convertView.getTag();
}
SettingOption option = values[position];
holder.check_title.setText(option.toString());
holder.check_image.setBackgroundDrawable(context.getResources().getDrawable(option.getDrawable()));
if(checkedItems[position]) {
holder.check_title.setChecked(true);
} else {
holder.check_title.setChecked(false);
}
return convertView;
}
class ViewHolder {
CheckedTextView check_title;
ImageView check_image;
}
}
私は何を間違っていますか?