有没有一种方法可以将数据保存在SharedPreference上?

时间:2020-06-13 21:17:41

标签: java android android-studio cs50

我正在尝试在按钮切换时保存数据,以便如果捕获到了神奇宝贝,则当我再次运行该应用程序时,神奇宝贝会被锁定,并且在释放时会切换回“捕获”。但是我所有的宠物小精灵都显示被抓到(按钮显示“释放”)

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”上未修改的源代码

1 个答案:

答案 0 :(得分:1)

下面的代码行

if (sharedpreferences.contains(nameTextView.getText().toString()))
            button.setText("Release");

应进入您的 onResponseListener(),即您的response -> ....

当前,您正在同步调用上述if语句。这意味着您不是在 SharedPreferences 中搜索任何有效的Pokemon名称,而是在搜索“”(空字符串)。在您的一项测试中,您可能将空字符串保存为键,并且您无法撤消该操作,因为加载口袋妖怪后的任何事务都包含非空字符串口袋妖怪名称。

如果确实如此,则可以轻松地在活动 onCreate()方法中添加临时代码,以从 SharedPreferences 中删除该条目。