有时候经常进行一些不连续的重复的操作.这就需要用到历史记录来快速的进行操作了.
关于历史记录的存储读写,我写了个小方法.如下:
point 是起指向作用,指向最旧的记录.比如有N条记录(最多),1~N,写入一条记录后point=(point+1)%N;读取时最新的记录在前面.
下面是存储路径的历史记录的实现方法:
List<String> readHistory(){
List<String> list= new ArrayList< String>();
Context ctx =this;
SharedPreferences SAVE = ctx.getSharedPreferences("save", MODE_PRIVATE);
int point=SAVE.getInt("point", 0);
String path;
final int N=16;
for(int i=0,n=point;i<=N;i++){
path=SAVE.getString("path"+n, null);
if(path!=null){
list.add(path);
}
n=n>0?(--n):(--n+N)%16;
}
return list;
}
Context ctx = this;
SharedPreferences SAVE = ctx.getSharedPreferences("save", MODE_PRIVATE);
int n=SAVE.getInt("point", 0);
Editor editor = SAVE.edit();
editor.putString("path"+n, path);
editor.putInt("point",( n+1)%16);
editor.commit();
}
最后用列表列出来即可.