[英]SharedPreferences default value
我在我的适配器中使用sharedpreferences,而不是获得默认值-1
,而是获得0
作为默认值。 谁能告诉我为什么会这样?
这是我的代码:
//constructor
public PlaylistAdapter(Activity a, ArrayList<Songs> songs) {
activity = a;
data = songs;
prefs = a.getSharedPreferences("com.myapp",
Context.MODE_PRIVATE);
db = new DatabaseHelper(a);
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(final int position, View convertView,
final ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.song_item, parent, false);
int playpos = prefs.getInt("posPlaying", -1);
//logs 0 as prefsvalue(should be -1) and the position
Log.v("--", prefs.getInt("posPlaying", -1) + " " + position);
}
如果posplaying不保存任何值,那么只会使用默认值。所以我认为posplaying会将值保存为0.检查是否将此值初始化为0或其他计算使posplaying为0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.