From 3222f7392e2c04c7b0f13e220dcabe3355020922 Mon Sep 17 00:00:00 2001 From: aminecmi Date: Wed, 8 Jul 2015 22:04:57 +0200 Subject: [PATCH] forecast --- .../amine/myterio/app/DetailsActivity.java | 18 ++++-- .../myterio/app/adapters/ForecastAdapter.java | 62 +++++++++++++++++++ .../myterio/app/model/ForecastWeather.java | 9 +++ app/src/main/res/layout/activity_details.xml | 2 +- app/src/main/res/layout/detail_item.xml | 22 +++++++ 5 files changed, 107 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/com/amine/myterio/app/adapters/ForecastAdapter.java create mode 100644 app/src/main/res/layout/detail_item.xml diff --git a/app/src/main/java/com/amine/myterio/app/DetailsActivity.java b/app/src/main/java/com/amine/myterio/app/DetailsActivity.java index 98f8add..0b64473 100644 --- a/app/src/main/java/com/amine/myterio/app/DetailsActivity.java +++ b/app/src/main/java/com/amine/myterio/app/DetailsActivity.java @@ -1,11 +1,14 @@ package com.amine.myterio.app; import android.os.Bundle; -import android.support.v7.app.ActionBarActivity; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; import android.widget.Toast; +import com.amine.myterio.app.adapters.ForecastAdapter; import com.amine.myterio.app.api.WeatherAdapters; import com.amine.myterio.app.api.WeatherApis; import com.amine.myterio.app.model.City; @@ -15,7 +18,7 @@ import retrofit.RetrofitError; import retrofit.client.Response; -public class DetailsActivity extends ActionBarActivity { +public class DetailsActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { @@ -26,6 +29,11 @@ public class DetailsActivity extends ActionBarActivity { WeatherAdapters adapters = new WeatherAdapters(); final Forecast[] f = {null}; + final RecyclerView list = (RecyclerView) findViewById(R.id.weekForecast); + LinearLayoutManager layoutManager = new LinearLayoutManager(this); + list.setLayoutManager(layoutManager); + list.setHasFixedSize(true); + if (cityName != null) { WeatherApis.WeatherDailyForecastLocationApi s = adapters.getWeatherForecastLocationAdapter(); @@ -34,6 +42,8 @@ public class DetailsActivity extends ActionBarActivity { @Override public void success(Forecast forecast, Response response) { f[0] = forecast; + list.setAdapter(new ForecastAdapter(DetailsActivity.this, f[0])); + } @Override @@ -50,6 +60,7 @@ public class DetailsActivity extends ActionBarActivity { @Override public void success(Forecast forecast, Response response) { f[0] = forecast; + list.setAdapter(new ForecastAdapter(DetailsActivity.this, f[0])); } @Override @@ -61,9 +72,6 @@ public class DetailsActivity extends ActionBarActivity { TextView name = (TextView) findViewById(R.id.cityName); name.setText(cityName); - - - } diff --git a/app/src/main/java/com/amine/myterio/app/adapters/ForecastAdapter.java b/app/src/main/java/com/amine/myterio/app/adapters/ForecastAdapter.java new file mode 100644 index 0000000..670d79a --- /dev/null +++ b/app/src/main/java/com/amine/myterio/app/adapters/ForecastAdapter.java @@ -0,0 +1,62 @@ +package com.amine.myterio.app.adapters; + +import android.content.Context; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; +import com.amine.myterio.app.R; +import com.amine.myterio.app.model.Forecast; +import com.amine.myterio.app.model.ForecastWeather; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + +public class ForecastAdapter extends RecyclerView.Adapter { + private static Forecast forecast; + private static Context c; + + public ForecastAdapter(Context c, Forecast forecast) { + this.forecast = forecast; + this.c = c; + } + + + @Override + public ForecastAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.detail_item, parent, false); + ViewHolder vh = new ViewHolder(v); + return vh; + } + + @Override + public void onBindViewHolder(ForecastAdapter.ViewHolder holder, int position) { + ForecastWeather fw = this.forecast.getList().get(position); + // MARCHE PAS + Date date = new Date(fw.getDt()); + DateFormat format = new SimpleDateFormat("EEEE"); + String finalDay = format.format(date); + + TextView dateView = (TextView) holder.itemView.findViewById(R.id.dateText); + TextView temp = (TextView) holder.itemView.findViewById(R.id.temp); + + dateView.setText(finalDay); + temp.setText(fw.getTemp().getDay() + ""); + } + + @Override + public int getItemCount() { + return forecast.getList().size(); + } + + public class ViewHolder extends RecyclerView.ViewHolder { + public View itemView; + + public ViewHolder(View itemView) { + super(itemView); + this.itemView = itemView; + } + } +} diff --git a/app/src/main/java/com/amine/myterio/app/model/ForecastWeather.java b/app/src/main/java/com/amine/myterio/app/model/ForecastWeather.java index 8b59d85..b92a0d2 100644 --- a/app/src/main/java/com/amine/myterio/app/model/ForecastWeather.java +++ b/app/src/main/java/com/amine/myterio/app/model/ForecastWeather.java @@ -6,6 +6,7 @@ public class ForecastWeather { public TempInfo temp; public List weather; public float speed; + public int dt; public TempInfo getTemp() { return temp; @@ -30,4 +31,12 @@ public class ForecastWeather { public void setSpeed(float speed) { this.speed = speed; } + + public int getDt() { + return dt; + } + + public void setDt(int dt) { + this.dt = dt; + } } diff --git a/app/src/main/res/layout/activity_details.xml b/app/src/main/res/layout/activity_details.xml index 11e22ed..0fc85a7 100644 --- a/app/src/main/res/layout/activity_details.xml +++ b/app/src/main/res/layout/activity_details.xml @@ -24,7 +24,7 @@ android:layout_centerHorizontal="true" android:layout_marginTop="20dp"/> - + + + + + + + \ No newline at end of file