Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SharedReferences默认值_Java_Android_Sharedpreferences_Android Preferences_Android Adapter - Fatal编程技术网

Java SharedReferences默认值

Java SharedReferences默认值,java,android,sharedpreferences,android-preferences,android-adapter,Java,Android,Sharedpreferences,Android Preferences,Android Adapter,我在适配器中使用SharedReferences,但不是获取默认值-1而是获取0作为默认值。谁能告诉我为什么会这样 这是我的密码: //constructor public PlaylistAdapter(Activity a, ArrayList<Songs> songs) { activity = a; data = songs; prefs = a.getSharedPreferences("com.myapp",

我在适配器中使用SharedReferences,但不是获取默认值
-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);
//构造函数
公共播放适配器(活动a,ArrayList歌曲){
活动=a;
数据=歌曲;
prefs=a.getSharedReferences(“com.myapp”,
上下文。模式(私人);
db=新数据库助手(a);
充气器=(充气器)活动
.getSystemService(上下文布局\充气机\服务);
}
公共视图getView(最终整型位置,视图转换视图,
最终视图组(父视图){
视图vi=转换视图;
if(convertView==null)
vi=充气机。充气(R.layout.song_项,父项,假);
int playpos=prefs.getInt(“posPlaying”,-1);
//将0记录为prefsvalue(应为-1)和位置
Log.v(“--”,prefs.getInt(“posPlaying”,-1)+“position”);

}

如果posplaying不包含任何值,那么只有默认值才会出现。因此,我认为posplaying将该值保留为0。请检查您是否使用0初始化该值或其他计算将该posplaying设置为0