From c4db4c26bb531974ba10c62185fd931e723a6859 Mon Sep 17 00:00:00 2001 From: Marcin Zelent Date: Fri, 20 Apr 2018 10:50:23 +0200 Subject: Added displaying of pictures --- .../liberavem/AllObservationsFragment.java | 32 +++++++++++++++------- .../liberavem/ObservationDetailsActivity.java | 6 +++- .../liberavem/ObservationsListAdapter.java | 14 ++++------ 3 files changed, 33 insertions(+), 19 deletions(-) (limited to 'app/src') diff --git a/app/src/main/java/org/marcinzelent/liberavem/AllObservationsFragment.java b/app/src/main/java/org/marcinzelent/liberavem/AllObservationsFragment.java index 3f3ad7b..e41b29e 100644 --- a/app/src/main/java/org/marcinzelent/liberavem/AllObservationsFragment.java +++ b/app/src/main/java/org/marcinzelent/liberavem/AllObservationsFragment.java @@ -58,20 +58,31 @@ public class AllObservationsFragment extends Fragment { Intent detailsIntent = new Intent(getActivity(), ObservationDetailsActivity.class); detailsIntent.putExtra("Observation", observations[position]); - //String photoUrl = ""; - //for (Bird bird : birds) - // if (bird.getId() == observations[position].getBirdId()) - // photoUrl = bird.getPhotoUrl(); - //detailsIntent.putExtra("Photo", photoUrl); + String photoUrl = ""; + for (Bird bird : birds) + if (bird.getId() == observations[position].getBirdId()) + photoUrl = bird.getPhotoUrl(); + detailsIntent.putExtra("Photo", photoUrl); startActivity(detailsIntent); } }); - //final ObservationsListAdapter adapter = new ObservationsListAdapter(getActivity(), observations, birds); - //observationsListView.setAdapter(adapter); + String birdsUrl = "http://birdobservationservice.azurewebsites.net/Service1.svc/birds"; + final StringRequest birdsRequest = new StringRequest(Request.Method.GET, birdsUrl, new Response.Listener() { + @Override + public void onResponse(String response) { + Gson gson = new GsonBuilder().create(); + birds = gson.fromJson(response, Bird[].class); + } + }, new com.android.volley.Response.ErrorListener() { + @Override + public void onErrorResponse(VolleyError error) { + Toast.makeText(getActivity(), "Couldn't connect to the database!", Toast.LENGTH_LONG).show(); + } + }); String observationsUrl = "http://birdobservationservice.azurewebsites.net/Service1.svc/observations"; - final StringRequest observationRequest = new StringRequest(Request.Method.GET, observationsUrl, new Response.Listener() { + final StringRequest observationsRequest = new StringRequest(Request.Method.GET, observationsUrl, new Response.Listener() { @Override public void onResponse(String response) { Gson gson = new GsonBuilder().create(); @@ -89,10 +100,11 @@ public class AllObservationsFragment extends Fragment { }, new com.android.volley.Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { - Toast.makeText(getActivity(), "Database connection error :( ", Toast.LENGTH_LONG).show(); + Toast.makeText(getActivity(), "Couldn't connect to the database!", Toast.LENGTH_LONG).show(); } }); - Volley.newRequestQueue(getActivity()).add(observationRequest); + Volley.newRequestQueue(getActivity()).add(birdsRequest); + Volley.newRequestQueue(getActivity()).add(observationsRequest); } } diff --git a/app/src/main/java/org/marcinzelent/liberavem/ObservationDetailsActivity.java b/app/src/main/java/org/marcinzelent/liberavem/ObservationDetailsActivity.java index 8373145..83e9c9a 100644 --- a/app/src/main/java/org/marcinzelent/liberavem/ObservationDetailsActivity.java +++ b/app/src/main/java/org/marcinzelent/liberavem/ObservationDetailsActivity.java @@ -5,6 +5,8 @@ import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; +import com.squareup.picasso.Picasso; + public class ObservationDetailsActivity extends AppCompatActivity { @Override @@ -14,8 +16,10 @@ public class ObservationDetailsActivity extends AppCompatActivity { Intent intent = getIntent(); Observation observation = (Observation) intent.getSerializableExtra("Observation"); - //String photoUrl = (String) intent.getSerializableExtra("Photo"); + String photoUrl = (String) intent.getSerializableExtra("Photo"); + SquareImageView detailsPhoto = findViewById(R.id.details_photo); + Picasso.get().load(photoUrl).into(detailsPhoto); TextView detailsName = findViewById(R.id.details_name); detailsName.setText(observation.getNameEnglish()); TextView detailsCreated = findViewById(R.id.details_created); diff --git a/app/src/main/java/org/marcinzelent/liberavem/ObservationsListAdapter.java b/app/src/main/java/org/marcinzelent/liberavem/ObservationsListAdapter.java index b73f103..b28e162 100644 --- a/app/src/main/java/org/marcinzelent/liberavem/ObservationsListAdapter.java +++ b/app/src/main/java/org/marcinzelent/liberavem/ObservationsListAdapter.java @@ -10,6 +10,8 @@ import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; +import com.squareup.picasso.Picasso; + public class ObservationsListAdapter extends BaseAdapter { @@ -66,19 +68,15 @@ public class ObservationsListAdapter extends BaseAdapter { viewHolder.date.setText(observations[position].getCreated()); String photoUrl = ""; - //for (Bird bird : birds) - // if (bird.getId() == observations[position].getBirdId()) - // photoUrl = bird.getPhotoUrl(); + for (Bird bird : birds) + if (bird.getId() == observations[position].getBirdId()) + photoUrl = bird.getPhotoUrl(); - //viewHolder.photo.setImageResource(photoUrl); + Picasso.get().load(photoUrl).into(viewHolder.photo); return convertView; } - public void add(int position, Observation observation, Bird bird) { - - } - private static class ViewHolder { TextView name; TextView date; -- cgit v1.2.3