added weather icon
This commit is contained in:
parent
de7528c3ec
commit
2f13747b8a
|
@ -1,20 +1,33 @@
|
|||
package at.fhj.swd.dailyhelper.adapter;
|
||||
|
||||
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.v7.widget.CardView;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
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 at.fhj.swd.dailyhelper.R;
|
||||
import at.fhj.swd.dailyhelper.model.Weather;
|
||||
|
||||
public class WeatherListAdapter extends RecyclerView.Adapter<WeatherListAdapter.WeatherItemViewHolder> {
|
||||
private ImageView imageView;
|
||||
|
||||
|
||||
private List<Weather> weathers;
|
||||
|
||||
|
@ -43,6 +56,11 @@ public class WeatherListAdapter extends RecyclerView.Adapter<WeatherListAdapter.
|
|||
});
|
||||
|
||||
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.tvTemperature.setText(context.getString(R.string.temperature) + ": " + weathers.get(i).getTemp() + "°C");
|
||||
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;
|
||||
|
||||
TextView tvDate;
|
||||
|
||||
TextView tvDescription;
|
||||
TextView tvTemperature;
|
||||
TextView tvHumity;
|
||||
|
@ -71,6 +90,10 @@ public class WeatherListAdapter extends RecyclerView.Adapter<WeatherListAdapter.
|
|||
cvWeatherItem = itemView.findViewById(R.id.cvWeatherItem);
|
||||
|
||||
tvDate = itemView.findViewById(R.id.tvDate);
|
||||
imageView = itemView.findViewById(R.id.card_view_image);
|
||||
|
||||
|
||||
|
||||
tvDescription = itemView.findViewById(R.id.tvDescription);
|
||||
tvTemperature = itemView.findViewById(R.id.tvTemperature);
|
||||
tvHumity = itemView.findViewById(R.id.tvHumity);
|
||||
|
@ -82,4 +105,30 @@ public class WeatherListAdapter extends RecyclerView.Adapter<WeatherListAdapter.
|
|||
public interface ItemClickListener {
|
||||
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() {
|
||||
WeatherListAdapter adapter = new WeatherListAdapter(WeatherDB.getDatabase(getContext()).weatherDAO().getAll(), this);
|
||||
|
||||
weatherList.setAdapter(adapter);
|
||||
}
|
||||
|
||||
|
||||
public void refresh(){
|
||||
|
||||
fetchLoctions();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -13,6 +13,8 @@
|
|||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvDate"
|
||||
android:text="Date: "
|
||||
|
@ -21,6 +23,13 @@
|
|||
android:layout_width="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
|
||||
android:id="@+id/tvDescription"
|
||||
android:text="Description:"
|
||||
|
@ -29,6 +38,7 @@
|
|||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvTemperature"
|
||||
android:text="Temp:"
|
||||
|
|
|
@ -16,4 +16,4 @@
|
|||
|
||||
|
||||
## 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