kyledavis124

Untitled

Jun 9th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.87 KB | None | 0 0
  1. package edu.harvard.cs50.pokedex;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.content.Context;
  6. import android.content.SharedPreferences;
  7. import android.os.Bundle;
  8. import android.util.Log;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.TextView;
  12.  
  13. import com.android.volley.Request;
  14. import com.android.volley.RequestQueue;
  15. import com.android.volley.Response;
  16. import com.android.volley.VolleyError;
  17. import com.android.volley.toolbox.JsonObjectRequest;
  18. import com.android.volley.toolbox.Volley;
  19.  
  20. import org.json.JSONArray;
  21. import org.json.JSONException;
  22. import org.json.JSONObject;
  23.  
  24. public class PokemonActivity extends AppCompatActivity {
  25.     private TextView nameTextView;
  26.     private TextView numberTextView;
  27.     private TextView type1TextView;
  28.     private TextView type2TextView;
  29.     private String url;
  30.     private RequestQueue requestQueue;
  31.     private boolean isCaught = false;
  32.     Button caughtButton;
  33.  
  34.     public String pokemonPreferences = "pokemonPreferences";
  35.     public String pokemonNameKey = "nameKey";
  36.     public String pokemonIsCaught = "isCaughtKey";
  37.  
  38.     SharedPreferences sharedPreferences;
  39.  
  40.  
  41.  
  42.     @Override
  43.     protected void onCreate(Bundle savedInstanceState) {
  44.         super.onCreate(savedInstanceState);
  45.         setContentView(R.layout.activity_pokemon);
  46.  
  47.         requestQueue = Volley.newRequestQueue(getApplicationContext());
  48.         url = getIntent().getStringExtra("url");
  49.         nameTextView = findViewById(R.id.pokemon_name);
  50.         numberTextView = findViewById(R.id.pokemon_number);
  51.         type1TextView = findViewById(R.id.pokemon_type1);
  52.         type2TextView = findViewById(R.id.pokemon_type2);
  53.         caughtButton = findViewById(R.id.pokemon_catch);
  54.  
  55.         sharedPreferences = getSharedPreferences(pokemonPreferences, Context.MODE_PRIVATE);
  56.         load();
  57.     }
  58.  
  59.     public void load() {
  60.         type1TextView.setText("");
  61.         type2TextView.setText("");
  62.  
  63.         JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
  64.             @Override
  65.             public void onResponse(JSONObject response) {
  66.                 try {
  67.                     nameTextView.setText(response.getString("name"));
  68.                     numberTextView.setText(String.format("#%03d", response.getInt("id")));
  69.  
  70.                     JSONArray typeEntries = response.getJSONArray("types");
  71.                     for (int i = 0; i < typeEntries.length(); i++) {
  72.                         JSONObject typeEntry = typeEntries.getJSONObject(i);
  73.                         int slot = typeEntry.getInt("slot");
  74.                         String type = typeEntry.getJSONObject("type").getString("name");
  75.  
  76.                         if (slot == 1) {
  77.                             type1TextView.setText(type);
  78.                         }
  79.                         else if (slot == 2) {
  80.                             type2TextView.setText(type);
  81.                         }
  82.                     }
  83.                 } catch (JSONException e) {
  84.                     Log.e("cs50", "Pokemon json error", e);
  85.                 }
  86.             }
  87.         }, new Response.ErrorListener() {
  88.             @Override
  89.             public void onErrorResponse(VolleyError error) {
  90.                 Log.e("cs50", "Pokemon details error", error);
  91.             }
  92.         });
  93.  
  94.         requestQueue.add(request);
  95.     }
  96.  
  97.     public void toggleCatch(View view) {
  98.  
  99.         SharedPreferences.Editor editor = sharedPreferences.edit();
  100.  
  101.         editor.putString(pokemonNameKey, nameTextView.toString());
  102.         editor.putBoolean(pokemonIsCaught,isCaught);
  103.         editor.commit();
  104.  
  105.         isCaught = !isCaught;
  106.  
  107.         caughtButton.setText(isCaught ? "Release" : "Catch");
  108.  
  109.     }
  110.  
  111.  
  112.  
  113. }
Add Comment
Please, Sign In to add comment