使用Spnner代替编辑框,只能选择已有的,不会越界,大大简化了代码。
String[] SA = new String[list.size()];
for (int i=0; i<SA.length; i++) {SA[i] = String.valueOf(i);
}
ArrayAdapter<String> adapter1 = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_spinner_item, SA);
Spinner spinner = new Spinner(MainActivity.this);
spinner.setAdapter(adapter1);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(android.R.drawable.ic_menu_edit);
builder.setTitle("交换:图" + position);
builder.setView(spinner);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which1) {int position1 = spinner.getSelectedItemPosition();if (position1 != position) {Collections.swap(list, position, position1);adapter.notifyDataSetChanged();Toast.makeText(MainActivity.this, "图" + position + " " + items[which] + "到 图" + position1, Toast.LENGTH_SHORT).show();saveList();}}
});
builder.setNegativeButton("取消", null);
builder.create().show();
源码:https://gitlink.org.cn/sonichy/PinTu