added weather icon
This commit is contained in:
parent
de7528c3ec
commit
2f13747b8a
|
@ -1,20 +1,33 @@
|
||||||
package at.fhj.swd.dailyhelper.adapter;
|
package at.fhj.swd.dailyhelper.adapter;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
import android.graphics.BitmapFactory;
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.os.AsyncTask;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
import android.support.v7.widget.CardView;
|
import android.support.v7.widget.CardView;
|
||||||
import android.support.v7.widget.RecyclerView;
|
import android.support.v7.widget.RecyclerView;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
import android.widget.ImageView;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URL;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import at.fhj.swd.dailyhelper.R;
|
import at.fhj.swd.dailyhelper.R;
|
||||||
import at.fhj.swd.dailyhelper.model.Weather;
|
import at.fhj.swd.dailyhelper.model.Weather;
|
||||||
|
|
||||||
public class WeatherListAdapter extends RecyclerView.Adapter<WeatherListAdapter.WeatherItemViewHolder> {
|
public class WeatherListAdapter extends RecyclerView.Adapter<WeatherListAdapter.WeatherItemViewHolder> {
|
||||||
|
private ImageView imageView;
|
||||||
|
|
||||||
|
|
||||||
private List<Weather> weathers;
|
private List<Weather> weathers;
|
||||||
|
|
||||||
|
@ -43,6 +56,11 @@ public class WeatherListAdapter extends RecyclerView.Adapter<WeatherListAdapter.
|
||||||
});
|
});
|
||||||
|
|
||||||
weatherItemViewHolder.tvDate.setText(weathers.get(i).getDate());
|
weatherItemViewHolder.tvDate.setText(weathers.get(i).getDate());
|
||||||
|
|
||||||
|
String icon = weathers.get(i).getIcon();
|
||||||
|
new DownloadImageTask(imageView).execute("https://openweathermap.org/img/w/" + icon + ".png");
|
||||||
|
|
||||||
|
|
||||||
weatherItemViewHolder.tvDescription.setText(weathers.get(i).getDescription());
|
weatherItemViewHolder.tvDescription.setText(weathers.get(i).getDescription());
|
||||||
weatherItemViewHolder.tvTemperature.setText(context.getString(R.string.temperature) + ": " + weathers.get(i).getTemp() + "°C");
|
weatherItemViewHolder.tvTemperature.setText(context.getString(R.string.temperature) + ": " + weathers.get(i).getTemp() + "°C");
|
||||||
weatherItemViewHolder.tvHumity.setText(context.getString(R.string.humidity) + ": " + weathers.get(i).getHumidity()+"%");
|
weatherItemViewHolder.tvHumity.setText(context.getString(R.string.humidity) + ": " + weathers.get(i).getHumidity()+"%");
|
||||||
|
@ -61,6 +79,7 @@ public class WeatherListAdapter extends RecyclerView.Adapter<WeatherListAdapter.
|
||||||
CardView cvWeatherItem;
|
CardView cvWeatherItem;
|
||||||
|
|
||||||
TextView tvDate;
|
TextView tvDate;
|
||||||
|
|
||||||
TextView tvDescription;
|
TextView tvDescription;
|
||||||
TextView tvTemperature;
|
TextView tvTemperature;
|
||||||
TextView tvHumity;
|
TextView tvHumity;
|
||||||
|
@ -71,6 +90,10 @@ public class WeatherListAdapter extends RecyclerView.Adapter<WeatherListAdapter.
|
||||||
cvWeatherItem = itemView.findViewById(R.id.cvWeatherItem);
|
cvWeatherItem = itemView.findViewById(R.id.cvWeatherItem);
|
||||||
|
|
||||||
tvDate = itemView.findViewById(R.id.tvDate);
|
tvDate = itemView.findViewById(R.id.tvDate);
|
||||||
|
imageView = itemView.findViewById(R.id.card_view_image);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
tvDescription = itemView.findViewById(R.id.tvDescription);
|
tvDescription = itemView.findViewById(R.id.tvDescription);
|
||||||
tvTemperature = itemView.findViewById(R.id.tvTemperature);
|
tvTemperature = itemView.findViewById(R.id.tvTemperature);
|
||||||
tvHumity = itemView.findViewById(R.id.tvHumity);
|
tvHumity = itemView.findViewById(R.id.tvHumity);
|
||||||
|
@ -82,4 +105,30 @@ public class WeatherListAdapter extends RecyclerView.Adapter<WeatherListAdapter.
|
||||||
public interface ItemClickListener {
|
public interface ItemClickListener {
|
||||||
void onItemClicked(Weather weather, int position);
|
void onItemClicked(Weather weather, int position);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
|
||||||
|
ImageView bmImage;
|
||||||
|
public DownloadImageTask(ImageView bmImage) {
|
||||||
|
this.bmImage = bmImage;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Bitmap doInBackground(String... urls) {
|
||||||
|
String urldisplay = urls[0];
|
||||||
|
Bitmap bmp = null;
|
||||||
|
try {
|
||||||
|
InputStream in = new java.net.URL(urldisplay).openStream();
|
||||||
|
bmp = BitmapFactory.decodeStream(in);
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.e("Error", e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return bmp;
|
||||||
|
}
|
||||||
|
protected void onPostExecute(Bitmap result) {
|
||||||
|
bmImage.setImageBitmap(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,15 +44,12 @@ public class WeatherFragment extends Fragment implements WeatherListAdapter.Item
|
||||||
|
|
||||||
private void fetchLoctions() {
|
private void fetchLoctions() {
|
||||||
WeatherListAdapter adapter = new WeatherListAdapter(WeatherDB.getDatabase(getContext()).weatherDAO().getAll(), this);
|
WeatherListAdapter adapter = new WeatherListAdapter(WeatherDB.getDatabase(getContext()).weatherDAO().getAll(), this);
|
||||||
|
|
||||||
weatherList.setAdapter(adapter);
|
weatherList.setAdapter(adapter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void refresh(){
|
public void refresh(){
|
||||||
|
|
||||||
fetchLoctions();
|
fetchLoctions();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -13,6 +13,8 @@
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content">
|
android:layout_height="wrap_content">
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/tvDate"
|
android:id="@+id/tvDate"
|
||||||
android:text="Date: "
|
android:text="Date: "
|
||||||
|
@ -21,6 +23,13 @@
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content" />
|
android:layout_height="wrap_content" />
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/card_view_image"
|
||||||
|
android:layout_width="100dp"
|
||||||
|
android:layout_height="100dp"
|
||||||
|
android:scaleType="fitXY" />
|
||||||
|
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/tvDescription"
|
android:id="@+id/tvDescription"
|
||||||
android:text="Description:"
|
android:text="Description:"
|
||||||
|
@ -29,6 +38,7 @@
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content" />
|
android:layout_height="wrap_content" />
|
||||||
|
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/tvTemperature"
|
android:id="@+id/tvTemperature"
|
||||||
android:text="Temp:"
|
android:text="Temp:"
|
||||||
|
|
|
@ -16,4 +16,4 @@
|
||||||
|
|
||||||
|
|
||||||
## Wetter Icon
|
## Wetter Icon
|
||||||
String iconUrl = "http://openweathermap.org/img/w/" + iconCode + ".png";
|
String iconUrl = "https://openweathermap.org/img/w/" + iconCode + ".png";
|
Loading…
Reference in New Issue