有没有办法将数据保存在 SharedPreference 上?
Is there a way to save the data on SharedPreference?
我试图在按钮切换上保存数据,这样如果抓到宠物小精灵,当我再次 运行 应用程序时,抓到宠物小精灵,当释放时,它切换回 "catch" .但是我所有的口袋妖怪都显示已捕获(按钮显示 "release")
package com.example.pokedex;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class PokemonActivity extends AppCompatActivity {
private TextView nameTextView;
private TextView numberTextView;
private TextView type1TextView;
private TextView type2TextView;
private String url;
private RequestQueue requestQueue;
Button button;
SharedPreferences sharedpreferences;
public static final String mypreference = "mypref";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pokemon);
requestQueue = Volley.newRequestQueue(getApplicationContext());
url = getIntent().getStringExtra("url");
nameTextView = findViewById(R.id.pokedex_name);
numberTextView = findViewById(R.id.pokedex_number);
type1TextView = findViewById(R.id.pokedex_type);
type2TextView = findViewById(R.id.pokedex_type2);
button = findViewById(R.id.catcher);
sharedpreferences = getSharedPreferences(mypreference,
Context.MODE_PRIVATE);
load();
}
public void load() {
type1TextView.setText("");
type2TextView.setText("");
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, response -> {
try {
nameTextView.setText(response.getString("name"));
numberTextView.setText(String.format("#%03d", response.getInt("id")));
JSONArray typeEntries = response.getJSONArray("types");
for (int i = 0; i < typeEntries.length(); i++) {
JSONObject typeEntry = typeEntries.getJSONObject(i);
int slot = typeEntry.getInt("slot");
String type = typeEntry.getJSONObject("type").getString("name");
if (slot == 1) {
type1TextView.setText(type);
} else if (slot == 2) {
type2TextView.setText(type);
}
}
} catch (JSONException e) {
Log.e("cs50", "Pokemon json error", e);
}
}, error -> Log.e("cs50", "Pokemon details error", error));
requestQueue.add(request);
if (sharedpreferences.contains(nameTextView.getText().toString()))
button.setText("Release");
}
public void toggleCatch(View view) {
String name = nameTextView.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
if (button.getText().toString().toLowerCase().equals("Catch".toLowerCase())) {
//editor.clear();
editor.putString(name, name);
editor.apply();
editor.commit();
button.setText("Release");
} else if (button.getText().toString().toLowerCase().equals("Release".toLowerCase())){
editor.remove(name);
editor.apply();
editor.commit();
button.setText("Catch");
}
}
}
我已经尽力了,请帮忙。
未修改的源代码位于“https://cdn.cs50.net/2019/fall/tracks/android/pokedex/pokedex.zip”
下面一行代码
if (sharedpreferences.contains(nameTextView.getText().toString()))
button.setText("Release");
应该进入你的 onResponseListener() 那就是你的 response -> ....
目前您正在同步调用上面的if语句。这意味着您在 SharedPreferences 中搜索的不是任何有效的 Pokemon 名称,而是“”(空字符串)。在您的一个测试中,您可能将空字符串保存为密钥,并且您无法撤消它,因为加载您的宠物小精灵后的任何交易都包含一个非空字符串宠物小精灵名称。
如果确实如此,您可以轻松地在 activity onCreate() 方法中添加一个临时代码,以从 中删除该条目SharedPreferences.
我试图在按钮切换上保存数据,这样如果抓到宠物小精灵,当我再次 运行 应用程序时,抓到宠物小精灵,当释放时,它切换回 "catch" .但是我所有的口袋妖怪都显示已捕获(按钮显示 "release")
package com.example.pokedex;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class PokemonActivity extends AppCompatActivity {
private TextView nameTextView;
private TextView numberTextView;
private TextView type1TextView;
private TextView type2TextView;
private String url;
private RequestQueue requestQueue;
Button button;
SharedPreferences sharedpreferences;
public static final String mypreference = "mypref";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pokemon);
requestQueue = Volley.newRequestQueue(getApplicationContext());
url = getIntent().getStringExtra("url");
nameTextView = findViewById(R.id.pokedex_name);
numberTextView = findViewById(R.id.pokedex_number);
type1TextView = findViewById(R.id.pokedex_type);
type2TextView = findViewById(R.id.pokedex_type2);
button = findViewById(R.id.catcher);
sharedpreferences = getSharedPreferences(mypreference,
Context.MODE_PRIVATE);
load();
}
public void load() {
type1TextView.setText("");
type2TextView.setText("");
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, response -> {
try {
nameTextView.setText(response.getString("name"));
numberTextView.setText(String.format("#%03d", response.getInt("id")));
JSONArray typeEntries = response.getJSONArray("types");
for (int i = 0; i < typeEntries.length(); i++) {
JSONObject typeEntry = typeEntries.getJSONObject(i);
int slot = typeEntry.getInt("slot");
String type = typeEntry.getJSONObject("type").getString("name");
if (slot == 1) {
type1TextView.setText(type);
} else if (slot == 2) {
type2TextView.setText(type);
}
}
} catch (JSONException e) {
Log.e("cs50", "Pokemon json error", e);
}
}, error -> Log.e("cs50", "Pokemon details error", error));
requestQueue.add(request);
if (sharedpreferences.contains(nameTextView.getText().toString()))
button.setText("Release");
}
public void toggleCatch(View view) {
String name = nameTextView.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
if (button.getText().toString().toLowerCase().equals("Catch".toLowerCase())) {
//editor.clear();
editor.putString(name, name);
editor.apply();
editor.commit();
button.setText("Release");
} else if (button.getText().toString().toLowerCase().equals("Release".toLowerCase())){
editor.remove(name);
editor.apply();
editor.commit();
button.setText("Catch");
}
}
}
我已经尽力了,请帮忙。
未修改的源代码位于“https://cdn.cs50.net/2019/fall/tracks/android/pokedex/pokedex.zip”
下面一行代码
if (sharedpreferences.contains(nameTextView.getText().toString()))
button.setText("Release");
应该进入你的 onResponseListener() 那就是你的 response -> ....
目前您正在同步调用上面的if语句。这意味着您在 SharedPreferences 中搜索的不是任何有效的 Pokemon 名称,而是“”(空字符串)。在您的一个测试中,您可能将空字符串保存为密钥,并且您无法撤消它,因为加载您的宠物小精灵后的任何交易都包含一个非空字符串宠物小精灵名称。
如果确实如此,您可以轻松地在 activity onCreate() 方法中添加一个临时代码,以从 中删除该条目SharedPreferences.