diff options
author | marcinzelent <zelent.marcin@gmail.com> | 2018-04-19 23:03:13 +0200 |
---|---|---|
committer | marcinzelent <zelent.marcin@gmail.com> | 2018-04-19 23:03:13 +0200 |
commit | 4e6fc4fcdbcc3c75be13b3f8b80c24e4648fe354 (patch) | |
tree | 9588d81ee5e3e470d242f82b015fac6e34d54014 /app/src/main/java/org/marcinzelent/liberavem/AllObservationsFragment.java | |
parent | 7294c6bd32e362b4c0a64cadbf4dd2c191e1ad8e (diff) |
Added details view, improved observations list
Diffstat (limited to 'app/src/main/java/org/marcinzelent/liberavem/AllObservationsFragment.java')
-rw-r--r-- | app/src/main/java/org/marcinzelent/liberavem/AllObservationsFragment.java | 73 |
1 files changed, 71 insertions, 2 deletions
diff --git a/app/src/main/java/org/marcinzelent/liberavem/AllObservationsFragment.java b/app/src/main/java/org/marcinzelent/liberavem/AllObservationsFragment.java index ee07682..3f3ad7b 100644 --- a/app/src/main/java/org/marcinzelent/liberavem/AllObservationsFragment.java +++ b/app/src/main/java/org/marcinzelent/liberavem/AllObservationsFragment.java @@ -1,18 +1,40 @@ package org.marcinzelent.liberavem; +import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.TextView; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.ListView; +import android.widget.Toast; + +import com.android.volley.Request; +import com.android.volley.RequestQueue; +import com.android.volley.Response; +import com.android.volley.VolleyError; +import com.android.volley.toolbox.StringRequest; +import com.android.volley.toolbox.Volley; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +import org.jetbrains.annotations.Nullable; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; +import java.util.TimeZone; /** * A simple {@link Fragment} subclass. */ public class AllObservationsFragment extends Fragment { - + Observation[] observations; + Bird[] birds; public AllObservationsFragment() { // Required empty public constructor @@ -26,4 +48,51 @@ public class AllObservationsFragment extends Fragment { return rootView; } + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + final ListView observationsListView = view.findViewById(R.id.observations_list_view); + + observationsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { + 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); + startActivity(detailsIntent); + } + }); + + //final ObservationsListAdapter adapter = new ObservationsListAdapter(getActivity(), observations, birds); + //observationsListView.setAdapter(adapter); + + String observationsUrl = "http://birdobservationservice.azurewebsites.net/Service1.svc/observations"; + final StringRequest observationRequest = new StringRequest(Request.Method.GET, observationsUrl, new Response.Listener<String>() { + @Override + public void onResponse(String response) { + Gson gson = new GsonBuilder().create(); + observations = gson.fromJson(response, Observation[].class); + for (Observation o : observations) { + int p = o.getCreated().indexOf('+'); + long epoch = Long.parseLong(o.getCreated().substring(6, p)); + Date date = new Date(epoch); + String formatted = new SimpleDateFormat("dd.MM.yyyy HH:mm", Locale.ENGLISH).format(date); + o.setCreated(formatted); + } + final ObservationsListAdapter adapter = new ObservationsListAdapter(getActivity(), observations, birds); + observationsListView.setAdapter(adapter); + } + }, new com.android.volley.Response.ErrorListener() { + @Override + public void onErrorResponse(VolleyError error) { + Toast.makeText(getActivity(), "Database connection error :( ", Toast.LENGTH_LONG).show(); + } + }); + + Volley.newRequestQueue(getActivity()).add(observationRequest); + } } |