我正在尝试将片段接收的数据保存到共享首选项中,以便在重新创建相同片段时可以重用存储在共享首选项中的数据。但不知何故,数据没有被保存,并且首选项的默认值被返回到我的片段。 下面是代码。 实施SharePreference的片段
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
public class StreamFragment extends Fragment {
public String streamUrl=null;
TextView textView;
public static final String playerData="pData";
SharedPreferences playerSettings;
public StreamFragment(){};
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
SharedPreferences playerSettings = getActivity().getSharedPreferences(playerData, Context.MODE_PRIVATE);
streamUrl = playerSettings.getString(streamUrl,"No Link Found");
}
@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_stream, container, false);
playerSettings = getActivity().getSharedPreferences(playerData, Context.MODE_PRIVATE);
streamUrl = playerSettings.getString("streamLink","No Link Found");
//getData();
Toast.makeText(getActivity(),"Stream Saved"+streamUrl,Toast.LENGTH_LONG).show();
return view;
}
public void getUrl(String data)
{
streamUrl=data;
playerSettings = this.getActivity().getSharedPreferences(playerData, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = playerSettings.edit();
editor.putString("streamLink", streamUrl);
editor.commit();
Toast.makeText(getActivity(),"Stream Saved"+streamUrl,Toast.LENGTH_LONG).show();
}
}
提前致谢!
答案 0 :(得分:2)
而不是streamUrl = playerSettings.getString(streamUrl,"No Link Found");
你应该使用
streamUrl = playerSettings.getString("streamLink","No Link Found");
并且还使用apply而不是commit
答案 1 :(得分:2)
我试着解释一下这一点:
参考此图片:
如您所见,onCreate
在onCreateview
之前被解雇。
让我们分析您的代码:
public class StreamFragment extends Fragment {
public String streamUrl=null;
TextView textView;
public static final String playerData="pData";
SharedPreferences playerSettings;
public StreamFragment(){};
此处,您的streamUrl
为null
。
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
/*SharedPreferences -- no needed*/playerSettings = getActivity().getSharedPreferences(playerData, Context.MODE_PRIVATE);
streamUrl = playerSettings.getString(/*streamUrl -- replace with a key value*/,"No Link Found");
}
这是第一种被解雇的方法。在这里,您尝试从String
获取sharedPref
,该null
应命名为getString
。这一点是错误的。 SharedPreferences
上的第一个参数应该是一个键,并且对于相同的值,唯一应该是
另一件事是你正在使用相同的名称来表示另一个 @Override
public View onCreateView( LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_stream, container, false);
//playerSettings = getActivity().getSharedPreferences(playerData, Context.MODE_PRIVATE); -- they are already istantiated
streamUrl = playerSettings.getString(/*streamUrl -- replace with a key value*/,"No Link Found");
//getData();
Toast.makeText(getActivity(),"Stream Saved"+streamUrl,Toast.LENGTH_LONG).show();
return view;
}
的距离,所以删除istantiation中的强制转换。
streamUrl
现在,您要将sharedPreferences's
设置为使用相同变量streamUrl
命名的streamUrl /*which is currently null*/ = playerSettings.getString(null, "No Link Found");
值。这意味着:
key
您仍应将streamUrl
从static unique field
替换为public void getUrl(String data)
{
streamUrl=data;
//playerSettings = this.getActivity().getSharedPreferences(playerData, Context.MODE_PRIVATE); -- already done above
SharedPreferences.Editor editor = playerSettings.edit();
editor.putString("streamLink", streamUrl);
//editor.commit(); --replace with apply
Toast.makeText(getActivity(),"Stream Saved"+streamUrl,Toast.LENGTH_LONG).show();
}
。
"streamLink"
}
这应该是您保存值的点,因此import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
public class StreamFragment extends Fragment {
public String streamUrl=null;
TextView textView;
public static final String playerData="pData";
public static final String valueKey="streamLink";
SharedPreferences playerSettings;
public StreamFragment(){};
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
playerSettings = getActivity().getSharedPreferences(playerData, Context.MODE_PRIVATE);
streamUrl = playerSettings.getString(valueKey,"No Link Found");
}
@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_stream, container, false);
streamUrl = playerSettings.getString(valueKey,"No Link Found");
//getData();
Toast.makeText(getActivity(),"Stream Saved"+streamUrl,Toast.LENGTH_LONG).show();
return view;
}
public void getUrl(String data)
{
streamUrl=data;
SharedPreferences.Editor editor = playerSettings.edit();
editor.putString(valueKey, streamUrl);
editor.apply();
Toast.makeText(getActivity(),"Stream Saved"+streamUrl,Toast.LENGTH_LONG).show();
}
}
应该是上面使用的密钥。
使用apply替换commit并删除istantiation,因为它已经完成
现在......说这应该是这样的:
echo 'system("ls *ZIP,*zip")' | ruby
希望这可以帮到你。
答案 2 :(得分:1)
您的偏好值的键不正确。请注意以下行。
streamUrl = playerSettings.getString(streamUrl,"No Link Found");
使用一些常量值作为键。
private static final String STREAM_URL = "stream_url";
并使用常量作为密钥,如下所述。
streamUrl = playerSettings.getString(STREAM_URL,"No Link Found");
答案 3 :(得分:1)
未调用getUrl(String)
方法。因此,您的共享偏好值未存储,在playerSettings.getString(streamUrl,"No Link Found");
之前调用它
您还要创建" SharedPreferences"的实例。两次,从onCreateView
中移除。使用editor.apply()
代替editor.commit()
。
答案 4 :(得分:0)
你正在使用两个SharedPreferences playerSettings;一个全局,一个在onCreate,你正在使用未初始化的上一个
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
public class StreamFragment extends Fragment {
public String streamUrl=null;
TextView textView;
public static final String playerData="pData";
SharedPreferences playerSettings;
public StreamFragment(){};
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
playerSettings = getActivity().getSharedPreferences(playerData, Context.MODE_PRIVATE);
streamUrl = playerSettings.getString("streamLink","No Link Found");
}
@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_stream, container, false);
playerSettings = getActivity().getSharedPreferences(playerData, Context.MODE_PRIVATE);
streamUrl = playerSettings.getString("streamLink","No Link Found");
//getData();
Toast.makeText(getActivity(),"Stream Saved"+streamUrl,Toast.LENGTH_LONG).show();
return view;
}
public void getUrl(String data)
{
streamUrl=data;
playerSettings = this.getActivity().getSharedPreferences(playerData, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = playerSettings.edit();
editor.putString("streamLink", streamUrl);
editor.commit();
Toast.makeText(getActivity(),"Stream Saved"+streamUrl,Toast.LENGTH_LONG).show();
}
}