From Flying Lion, 1 Week ago, written in Text.
This paste is a reply to Untitled from Ample Panda - view diff
Embed
  1. package com.example.t00533766.finalexamt00533766;
  2.  
  3. import android.content.Intent;
  4. import android.content.res.Resources;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.widget.AdapterView;
  10. import android.widget.GridView;
  11.  
  12. import org.json.JSONArray;
  13. import org.json.JSONException;
  14. import org.json.JSONObject;
  15.  
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.util.ArrayList;
  19. import java.util.HashMap;
  20.  
  21. public class MainActivity extends AppCompatActivity {
  22.  
  23.     public static final String SCIENTFIC_NAME = "scientific_name";
  24.     public static final String COMMON_NAME = "common_name";
  25.     public static final String DESCRIPTION = "description";
  26.     public static final String IMAGE = "image";
  27.     public static final String TREE_DATA_KEY = "treeData";
  28.     public static final String TREE_ICON_KEY = "treeIcon";
  29.     public ArrayList<Integer> largeTreeIcons = new ArrayList<Integer>();
  30.  
  31.     @Override
  32.     protected void onCreate(Bundle savedInstanceState) {
  33.         super.onCreate(savedInstanceState);
  34.         setContentView(R.layout.activity_main);
  35.         GridView gridView = findViewById(R.id.grid);
  36.         final GridAdapter gridAdapter = new GridAdapter(getApplicationContext());
  37.  
  38.         try {
  39.             JSONObject obj = new JSONObject(loadJSONFromAsset());
  40.             JSONArray jsonArray = obj.getJSONArray("trees");
  41.             HashMap<String, String> treeObject;
  42.  
  43.             for (int i = 0; i < jsonArray.length(); i++) {
  44.                 JSONObject arrayObject = jsonArray.getJSONObject(i);
  45.                 String scientificName = arrayObject.getString(SCIENTFIC_NAME);
  46.                 String commonName = arrayObject.getString(COMMON_NAME);
  47.                 String description = arrayObject.getString(DESCRIPTION);
  48.                 String treeImage = arrayObject.getString(IMAGE);
  49.  
  50.                 Log.d("name-->", scientificName+"\n"+ commonName+"\n"+description+"\n"+treeImage );
  51.  
  52.                 treeObject = new HashMap<String, String>();
  53.                 treeObject.put(SCIENTFIC_NAME, scientificName);
  54.                 treeObject.put(DESCRIPTION, description);
  55.                 treeObject.put(COMMON_NAME, commonName);
  56.                 gridAdapter.treesInfo.add(treeObject);
  57.  
  58.                 Resources resources = getResources();
  59.  
  60.                 int treeImageID = resources.getIdentifier(treeImage+"_small", "drawable", getPackageName());
  61.                 gridAdapter.smallTreeIcons.add(treeImageID);
  62.                 int largeTreeImageID = resources.getIdentifier(treeImage+"_big", "drawable", getPackageName());
  63.                 largeTreeIcons.add(largeTreeImageID);
  64.             }
  65.         } catch (JSONException e) {
  66.             e.printStackTrace();
  67.         }
  68.         gridView.setAdapter(gridAdapter);
  69.  
  70.         gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  71.             @Override
  72.             public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
  73.                 Intent intent = new Intent(getApplicationContext(),TreeDetailActivity.class);
  74.                 intent.putExtra(TREE_DATA_KEY,gridAdapter.treesInfo.get(i));
  75.                 intent.putExtra(TREE_ICON_KEY,largeTreeIcons.get(i));
  76.                 startActivity(intent);
  77.             }
  78.         });
  79.  
  80.     }
  81.  
  82.  
  83.     public String loadJSONFromAsset() {
  84.         String json = null;
  85.         try {
  86.             InputStream is = getAssets().open("potato.json");
  87.             int size = is.available();
  88.             byte[] buffer = new byte[size];
  89.             is.read(buffer);
  90.             is.close();
  91.             json = new String(buffer, "UTF-8");
  92.         } catch (IOException ex) {
  93.             ex.printStackTrace();
  94.             return null;
  95.         }
  96.         return json;
  97.     }
  98. }
  99.