【问题标题】:SharedPreferences default valueSharedPreferences 默认值
【发布时间】:2013-12-16 13:16:03
【问题描述】:

我在我的适配器中使用 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);

}

【问题讨论】:

    标签: java android sharedpreferences android-preferences android-adapter


    【解决方案1】:

    如果 posplaying 不包含任何值,那么只会出现默认值。所以我认为 posplaying 将值保持为 0。检查您是否使用 0 或其他一些计算将这个 posplaying 设置为 0。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多