需求:文件夹排序需要保存设置
要求:与 PreferenceScreen 共享
一.全局赋值
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
二.获取值
int order = sharedPreferences.getInt("order", 0);
三.单选对话框设置后重排并保存设置
String[] items = { "文件名", "时间", "大小", "文件名(倒序)", "时间(倒序)", "大小(倒序)" };
AlertDialog dialog = new AlertDialog.Builder(this);
dialog.setTitle("排序");
dialog.setSingleChoiceItems(items1, order, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
order = which;
Toast.makeText(MainActivity.this, order + "按 " + items[order] + " 排序", Toast.LENGTH_SHORT).show();
genList(path);
dialog.dismiss();
//保存
SharedPreferences.Editor SPE = sharedPreferences.edit();
SPE.putInt("order", order);
SPE.commit();
}
});