我在OnCreate中调用一个方法如下
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.game); GetScore();该方法在此定义
private void GetScore() { AsyncHttpClient client = new AsyncHttpClient(); RequestParams params = new RequestParams(); params.put("email", textEmail); client.get("http://techcube.pk/game/getScore.php", params, new `enter code here`AsyncHttpResponseHandler() { @Override public void onSuccess(String s) { super.onSuccess("Success"); String score = null; try { Log.d("Success", s); JSONObject json = new JSONObject(s); score = json.getString("Score"); } catch (Exception e) { e.printStackTrace(); } //Toast.makeText(getApplicationContext(), score, Toast.LENGTH_LONG).show(); fetchlivescore.setText(score); } @Override public void onFailure(Throwable throwable) { super.onFailure(throwable); Toast.makeText(getApplicationContext(), "Server Response for onFailure ", Toast.LENGTH_LONG).show(); } }); }现在我要做的是将json字符串的值存储在onSuccess方法内的fetchlivescore textview中,但我也想在Oncreate方法上使用这个值,即当我下次打开应用程序时显示toast或包含与Onsuccess方法中存储的值相同。 我怎么能实现这个目标? 我已经尝试了很多东西,但没有任何作用,因为下次在oncreate方法中没有显示存储的值,它总是显示为空
I am calling a method in OnCreate as following
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.game); GetScore();The method is defined here
private void GetScore() { AsyncHttpClient client = new AsyncHttpClient(); RequestParams params = new RequestParams(); params.put("email", textEmail); client.get("http://techcube.pk/game/getScore.php", params, new `enter code here`AsyncHttpResponseHandler() { @Override public void onSuccess(String s) { super.onSuccess("Success"); String score = null; try { Log.d("Success", s); JSONObject json = new JSONObject(s); score = json.getString("Score"); } catch (Exception e) { e.printStackTrace(); } //Toast.makeText(getApplicationContext(), score, Toast.LENGTH_LONG).show(); fetchlivescore.setText(score); } @Override public void onFailure(Throwable throwable) { super.onFailure(throwable); Toast.makeText(getApplicationContext(), "Server Response for onFailure ", Toast.LENGTH_LONG).show(); } }); }Now what i want to do is that i store the value of json string in fetchlivescore textview inside onSuccess method but i want to use this value also on the Oncreate method i.e. when i open the app next time it show a toast or a textview containing the same value that was stored in the Onsuccess method. how can i achieve this? i have tried many things but nothing works because the stored value is not showing next time in oncreate method and it always show empty
最满意答案
保存分数的一种方法是在onSuccess()方法中使用SharedPreferences。
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); editor.putString("key_score", score); editor.commit();在onCreate()您只需读取存储的值。
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); String score = sharedPref.getString("key_score");如果你在一个Activity中 - 使用this而不是getActivity() 。
One way to save the score would be to use SharedPreferences in your onSuccess() method.
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); editor.putString("key_score", score); editor.commit();And in onCreate() you just read the value stored.
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); String score = sharedPref.getString("key_score");If you're inside an Activity - use this instead of getActivity().
更多推荐
发布评论