@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Get the Species Type: "Shark," "Billfish," etc. jsonItem = this.getArguments().getString("type"); Log.d("KENNY", "String argument = " + jsonItem); try { JsonStorage jStore = new JsonStorage(getActivity()); String temp = jStore.loadSpeciesJSON(); populateItem(temp); } catch (IOException | JSONException e) { e.printStackTrace(); } final View myInflatedView = inflater.inflate(R.layout.fragment_species, container, false); /** * ************************************************************************** ************** * Favorite a fish ******************************************** * ************************************************************************** */ Button favorite = (Button) myInflatedView.findViewById(R.id.favorite); favorite.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { storeFavorite(myInflatedView); } }); /** * ************************************************************************** *************** * Log a fish ************************************************ * ************************************************************************** */ Button log = (Button) myInflatedView.findViewById(R.id.log); log.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { TextView text = (TextView) myInflatedView.findViewById(R.id.species_name); Calendar c = Calendar.getInstance(); String formattedDate = new SimpleDateFormat("MMM-dd-yyyy").format(c.getTime()); Intent intent = new Intent(getActivity(), LogActivity.class); intent.putExtra("speciesName", text.getText()); intent.putExtra("currDate", formattedDate); intent.putExtra("title", "Log a Fish"); startActivity(intent); } }); // Set the top text (above all the pics) to typeface and to this text TextView typeTextView = (TextView) myInflatedView.findViewById(R.id.type); StringBuilder typeString = new StringBuilder(); // Create string to capitalize first letters String[] typeArray = jsonItem.split("_"); // Split string by underscores for (int i = 0; i < typeArray.length; i++) { // Loop through only if multiple words String temp = typeArray[i]; // Get the first word typeString.append( temp.substring(0, 1).toUpperCase()); // Capitalize the first letter and append typeString.append( temp.substring( 1, temp.length())); // Append the rest of the word to capitalized first letter if (typeArray.length - i > 1) typeString.append(" "); // Put a space if there are multiple words. } typeTextView.setText(typeString.toString()); Typeface typeTypeFace = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Walkway_Bold.ttf"); typeTextView.setTypeface(typeTypeFace); // Loop through each species of the specified type and populate below the top text for (int i = 0; i < jsonArray.size(); i++) { // Get the linear layout and set the parameters LinearLayout ll = (LinearLayout) myInflatedView.findViewById(R.id.species_ll); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); params.gravity = Gravity.CENTER; // Use this for each image in the list final ImageView image = new ImageView(getActivity()); image.setAdjustViewBounds(true); // Scales it to the screen image.setId(i); // Sets each with unique id ll.addView(image, params); // Adds the ImageView to screen BEFORE adding image (important) Picasso.with(getActivity()) // THEN you add the image from photosList .load(photosList.get(i)) .into(image); // Next, set click listener. It makes a different view appear ABOVE the species images image.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { populateList(myInflatedView, image); } }); } return myInflatedView; }
/** * ************************************************************************************************************************************** * STORE FAVORITES * ************************************************************************************************************************************** */ private void storeFavorite(View view) { int imgPosition = -1; for (int i = 0; i < jsonArray.size(); i++) { TextView tv = (TextView) view.findViewById(R.id.species_name); String name = tv.getText().toString(); if (jsonArray.get(i).getName().equals(name)) imgPosition = i; } // =================================================================== // Step 1: Get all text views and the image // =================================================================== TextView speciesName = (TextView) view.findViewById(R.id.species_name); TextView scientific = (TextView) view.findViewById(R.id.scientific); TextView common = (TextView) view.findViewById(R.id.common); Log.d("KENNY", "Image Position: " + String.valueOf(imgPosition)); Log.d("KENNY", "Photos List size: " + String.valueOf(photosList.size())); String favoriteImage = photosList.get(imgPosition); TextView individual = (TextView) view.findViewById(R.id.individual); TextView aggregate = (TextView) view.findViewById(R.id.aggregate); TextView minimum = (TextView) view.findViewById(R.id.minimum); TextView season = (TextView) view.findViewById(R.id.season); TextView record = (TextView) view.findViewById(R.id.record); // =================================================================== // Step 2: Store all information in a single json object // =================================================================== JSONObject favorite = new JSONObject(); try { favorite.put("scientific", scientific.getText().toString()); favorite.put("common", common.getText().toString()); favorite.put("image", favoriteImage); favorite.put("individual", individual.getText().toString()); favorite.put("aggregate", aggregate.getText().toString()); favorite.put("minimum", minimum.getText().toString()); favorite.put("season", season.getText().toString()); favorite.put("record", record.getText().toString()); } catch (JSONException e) { Toast.makeText( getActivity(), "Could not save your favorite fish at this time...", Toast.LENGTH_SHORT) .show(); } // =================================================================== // Step 3: Get the json array and add it, or make a new one if necessary. // Then write it to the new json text. // =================================================================== JsonStorage json = new JsonStorage(getActivity()); boolean isOutermostThere = false; try { String favoriteJson = json.readProfileJSON(); JSONObject object = new JSONObject(favoriteJson); JSONObject outer = object.getJSONObject("outermost"); // Get outermost json object (everything) isOutermostThere = true; // There is a JSON object already made JSONObject favorites = outer.getJSONObject("favorites"); // Get the "favorites" JSONObject favorites.put( speciesName.getText().toString(), favorite); // Add it to the favorites object list outer.put("favorites", favorites); // Overwrite the favorites list with new one object.put("outermost", outer); // Add outermost to the JSON object json.writeJSON(object); // Populate this in the memory Toast.makeText(getActivity(), "You have added a favorite!", Toast.LENGTH_SHORT).show(); } catch (IOException | JSONException e) { // ********* Go here if the array did not already exist ***********// JSONObject newFavList = new JSONObject(); // Create new JSONObject try { newFavList.put(speciesName.getText().toString(), favorite); // Add the first entry if (isOutermostThere) { // If there is a JSONObject already made String favoriteJson = json.readProfileJSON(); // Load the populated data JSONObject object = new JSONObject(favoriteJson); // Get the whole json object JSONObject outer = object.getJSONObject("outermost"); // Get outermost object outer.put("favorites", newFavList); // Add new favorites array to the profile object object.put("outermost", outer); json.writeJSON(object); // Write this to the memory } else { JSONObject favorites = new JSONObject(); // Create new favorites object favorites.put("favorites", newFavList); // Add favorites list to favorites JSON object JSONObject outer = new JSONObject(); // Create new outermost JSON since it doesnt exist outer.put("outermost", favorites); // Store new favorites list into it. json.writeJSON(outer); } Toast.makeText( getActivity(), "You have added your first favorite fish!", Toast.LENGTH_SHORT) .show(); } catch (IOException | JSONException e1) { Toast.makeText( getActivity(), "Something went wrong saving. Please try again later.", Toast.LENGTH_SHORT) .show(); e1.printStackTrace(); } } }