diff options
Diffstat (limited to 'app/src/main/java/org/marcinzelent')
9 files changed, 378 insertions, 24 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); + } } diff --git a/app/src/main/java/org/marcinzelent/liberavem/Bird.java b/app/src/main/java/org/marcinzelent/liberavem/Bird.java index 668059c..a94faf3 100644 --- a/app/src/main/java/org/marcinzelent/liberavem/Bird.java +++ b/app/src/main/java/org/marcinzelent/liberavem/Bird.java @@ -1,37 +1,51 @@ package org.marcinzelent.liberavem; -public class Bird { - private int Id; - private long Created; - private String NameEnglish; - private String NameDanish; - private String PhotoUrl; - - public Bird(int id, long created, String nameEnglish, String nameDanish, String photoUrl) { - Id = id; - Created = created; - NameEnglish = nameEnglish; - NameDanish = nameDanish; - PhotoUrl = photoUrl; +import com.google.gson.annotations.SerializedName; + +import java.io.Serializable; + +public class Bird implements Serializable { + @SerializedName("Id") + private int id; + @SerializedName("Created") + private String created; + @SerializedName("NameEnglish") + private String nameEnglish; + @SerializedName("NameDanish") + private String nameDanish; + @SerializedName("PhotoUrl") + private String photoUrl; + + public Bird(int id, String created, String nameEnglish, String nameDanish, String photoUrl) { + this.id = id; + this.created = created; + this.nameEnglish = nameEnglish; + this.nameDanish = nameDanish; + this.photoUrl = photoUrl; } public int getId() { - return Id; + return id; } - public long getCreated() { - return Created; + public String getCreated() { + return created; } public String getNameEnglish() { - return NameEnglish; + return nameEnglish; } public String getNameDanish() { - return NameDanish; + return nameDanish; } public String getPhotoUrl() { - return PhotoUrl; + return photoUrl; + } + + @Override + public String toString() { + return getNameEnglish(); } } diff --git a/app/src/main/java/org/marcinzelent/liberavem/MainActivity.java b/app/src/main/java/org/marcinzelent/liberavem/MainActivity.java index dc261c5..b1c73b4 100644 --- a/app/src/main/java/org/marcinzelent/liberavem/MainActivity.java +++ b/app/src/main/java/org/marcinzelent/liberavem/MainActivity.java @@ -17,6 +17,14 @@ import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; +import android.widget.Toast; + +import com.android.volley.Request; +import com.android.volley.Response; +import com.android.volley.VolleyError; +import com.android.volley.toolbox.StringRequest; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, @@ -51,6 +59,7 @@ public class MainActivity extends AppCompatActivity toggle.syncState(); NavigationView navigationView = findViewById(R.id.nav_view); + navigationView.getMenu().getItem(0).setChecked(true); navigationView.setNavigationItemSelectedListener(this); } diff --git a/app/src/main/java/org/marcinzelent/liberavem/MyObservationsFragment.java b/app/src/main/java/org/marcinzelent/liberavem/MyObservationsFragment.java index bcc9067..1d27e99 100644 --- a/app/src/main/java/org/marcinzelent/liberavem/MyObservationsFragment.java +++ b/app/src/main/java/org/marcinzelent/liberavem/MyObservationsFragment.java @@ -1,11 +1,13 @@ 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.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; @@ -18,7 +20,8 @@ import java.util.ArrayList; * A simple {@link Fragment} subclass. */ public class MyObservationsFragment extends Fragment { - + Observation[] observations; + Bird[] birds; public MyObservationsFragment() { // Required empty public constructor @@ -40,6 +43,22 @@ public class MyObservationsFragment extends Fragment { "String 3" }; 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); + } + }); + ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, list); observationsListView.setAdapter(adapter); } diff --git a/app/src/main/java/org/marcinzelent/liberavem/Observation.java b/app/src/main/java/org/marcinzelent/liberavem/Observation.java new file mode 100644 index 0000000..be02812 --- /dev/null +++ b/app/src/main/java/org/marcinzelent/liberavem/Observation.java @@ -0,0 +1,93 @@ +package org.marcinzelent.liberavem; + +import com.google.gson.annotations.SerializedName; + +import java.io.Serializable; + +public class Observation implements Serializable { + @SerializedName("Id") + private int id; + @SerializedName("Created") + private String created; + @SerializedName("Latitude") + private double latitude; + @SerializedName("Longitude") + private double longitude; + @SerializedName("Placename") + private String placeName; + @SerializedName("Comment") + private String comment; + @SerializedName("UserId") + private String userId; + @SerializedName("BirdId") + private int birdId; + @SerializedName("NameEnglish") + private String nameEnglish; + @SerializedName("NameDanish") + private String nameDanish; + @SerializedName("Population") + private int population; + + public Observation(int id, String created, double latitude, double longitude, String placeName, + String comment, String userId, int birdId, String nameEnglish, + String nameDanish, int population) { + this.id = id; + this.created = created; + this.latitude = latitude; + this.longitude = longitude; + this.placeName = placeName; + this.comment = comment; + this.userId = userId; + this.birdId = birdId; + this.nameEnglish = nameEnglish; + this.nameDanish = nameDanish; + this.population = population; + } + + public int getId() { + return id; + } + + public String getCreated() { + return created; + } + public void setCreated(String created) { + this.created = created; + } + + public double getLatitude() { + return latitude; + } + + public double getLongitude() { + return longitude; + } + + public String getPlaceName() { + return placeName; + } + + public String getComment() { + return comment; + } + + public String getUserId() { + return userId; + } + + public int getBirdId() { + return birdId; + } + + public String getNameEnglish() { + return nameEnglish; + } + + public String getNameDanish() { + return nameDanish; + } + + public int getPopulation() { + return population; + } +} diff --git a/app/src/main/java/org/marcinzelent/liberavem/ObservationDetailsActivity.java b/app/src/main/java/org/marcinzelent/liberavem/ObservationDetailsActivity.java new file mode 100644 index 0000000..8373145 --- /dev/null +++ b/app/src/main/java/org/marcinzelent/liberavem/ObservationDetailsActivity.java @@ -0,0 +1,36 @@ +package org.marcinzelent.liberavem; + +import android.content.Intent; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.widget.TextView; + +public class ObservationDetailsActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_observation_details); + + Intent intent = getIntent(); + Observation observation = (Observation) intent.getSerializableExtra("Observation"); + //String photoUrl = (String) intent.getSerializableExtra("Photo"); + + TextView detailsName = findViewById(R.id.details_name); + detailsName.setText(observation.getNameEnglish()); + TextView detailsCreated = findViewById(R.id.details_created); + detailsCreated.setText(observation.getCreated()); + TextView detailsUser = findViewById(R.id.details_user); + detailsUser.setText(observation.getUserId()); + TextView detailsPopulation = findViewById(R.id.details_population); + detailsPopulation.setText(String.valueOf(observation.getPopulation())); + TextView detailsPlacename = findViewById(R.id.details_placename); + detailsPlacename.setText(observation.getPlaceName()); + TextView detailsLongitude = findViewById(R.id.details_longitude); + detailsLongitude.setText(String.valueOf(observation.getLongitude())); + TextView detailsLatitude = findViewById(R.id.details_latitude); + detailsLatitude.setText(String.valueOf(observation.getLatitude())); + TextView detailsComment = findViewById(R.id.details_comment); + if(observation.getComment() != null) detailsComment.setText(observation.getComment()); + } +} diff --git a/app/src/main/java/org/marcinzelent/liberavem/ObservationsFragment.java b/app/src/main/java/org/marcinzelent/liberavem/ObservationsFragment.java index aefaaf4..b093461 100644 --- a/app/src/main/java/org/marcinzelent/liberavem/ObservationsFragment.java +++ b/app/src/main/java/org/marcinzelent/liberavem/ObservationsFragment.java @@ -8,12 +8,10 @@ import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentManager; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.ListView; /** diff --git a/app/src/main/java/org/marcinzelent/liberavem/ObservationsListAdapter.java b/app/src/main/java/org/marcinzelent/liberavem/ObservationsListAdapter.java new file mode 100644 index 0000000..b73f103 --- /dev/null +++ b/app/src/main/java/org/marcinzelent/liberavem/ObservationsListAdapter.java @@ -0,0 +1,88 @@ +package org.marcinzelent.liberavem; + +import android.content.Context; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.ImageView; +import android.widget.TextView; + + +public class ObservationsListAdapter extends BaseAdapter { + + private Context context; + private final Observation[] observations; + private final Bird[] birds; + + public ObservationsListAdapter(Context context, Observation[] observations, Bird[] birds){ + //super(context, R.layout.single_list_app_item, utilsArrayList); + this.context = context; + this.observations = observations; + this.birds = birds; + } + + @Override + public int getCount() { + return observations.length; + } + + @Override + public Object getItem(int i) { + return i; + } + + @Override + public long getItemId(int i) { + return i; + } + + @NonNull + @Override + public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { + ViewHolder viewHolder; + final View result; + + if (convertView == null) { + + viewHolder = new ViewHolder(); + LayoutInflater inflater = LayoutInflater.from(context); + convertView = inflater.inflate(R.layout.observations_list_item, parent, false); + viewHolder.name = convertView.findViewById(R.id.oli_name); + viewHolder.date = convertView.findViewById(R.id.oli_date); + viewHolder.photo = convertView.findViewById(R.id.oli_photo); + + result = convertView; + + convertView.setTag(viewHolder); + } else { + viewHolder = (ViewHolder) convertView.getTag(); + result = convertView; + } + + viewHolder.name.setText(observations[position].getNameEnglish()); + viewHolder.date.setText(observations[position].getCreated()); + + String photoUrl = ""; + //for (Bird bird : birds) + // if (bird.getId() == observations[position].getBirdId()) + // photoUrl = bird.getPhotoUrl(); + + //viewHolder.photo.setImageResource(photoUrl); + + return convertView; + } + + public void add(int position, Observation observation, Bird bird) { + + } + + private static class ViewHolder { + TextView name; + TextView date; + ImageView photo; + } + +}
\ No newline at end of file diff --git a/app/src/main/java/org/marcinzelent/liberavem/SquareImageView.java b/app/src/main/java/org/marcinzelent/liberavem/SquareImageView.java new file mode 100644 index 0000000..ed5dbb7 --- /dev/null +++ b/app/src/main/java/org/marcinzelent/liberavem/SquareImageView.java @@ -0,0 +1,28 @@ +package org.marcinzelent.liberavem; + +import android.content.Context; +import android.util.AttributeSet; + +public class SquareImageView extends android.support.v7.widget.AppCompatImageView { + + public SquareImageView(Context context) { + super(context); + } + + public SquareImageView(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public SquareImageView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + + int width = getMeasuredWidth(); + setMeasuredDimension(width, width); + } + +} |