From 8340bbb48c10acf2b58e9d04d3922647917c7f4d Mon Sep 17 00:00:00 2001 From: aminecmi Date: Sun, 12 Jul 2015 17:47:59 +0200 Subject: [PATCH] search frag --- .../com/amine/myterio/app/SearchActivity.java | 66 +------------- .../myterio/app/fragments/SearchFragment.java | 89 +++++++++++++++++++ .../main/res/layout-large/activity_search.xml | 30 ++----- .../main/res/layout-large/search_fragment.xml | 27 ++++++ .../res/layout-normal/activity_search.xml | 30 ++----- .../res/layout-normal/search_fragment.xml | 27 ++++++ .../main/res/layout-small/activity_search.xml | 30 ++----- .../main/res/layout-small/search_fragment.xml | 27 ++++++ .../res/layout-xlarge/activity_search.xml | 26 ++---- .../res/layout-xlarge/search_fragment.xml | 27 ++++++ app/src/main/res/layout/activity_search.xml | 4 - app/src/main/res/values-en/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 13 files changed, 234 insertions(+), 151 deletions(-) create mode 100644 app/src/main/java/com/amine/myterio/app/fragments/SearchFragment.java create mode 100644 app/src/main/res/layout-large/search_fragment.xml create mode 100644 app/src/main/res/layout-normal/search_fragment.xml create mode 100644 app/src/main/res/layout-small/search_fragment.xml create mode 100644 app/src/main/res/layout-xlarge/search_fragment.xml diff --git a/app/src/main/java/com/amine/myterio/app/SearchActivity.java b/app/src/main/java/com/amine/myterio/app/SearchActivity.java index 7c272aa..e2d6a80 100644 --- a/app/src/main/java/com/amine/myterio/app/SearchActivity.java +++ b/app/src/main/java/com/amine/myterio/app/SearchActivity.java @@ -1,82 +1,18 @@ package com.amine.myterio.app; -import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; -import android.view.View; -import android.widget.*; -import com.amine.myterio.app.api.CitiesAdapters; -import com.amine.myterio.app.api.CitiesApi; -import com.amine.myterio.app.config.Config; -import com.amine.myterio.app.model.Countries; -import com.amine.myterio.app.model.Predictions; -import retrofit.Callback; -import retrofit.RetrofitError; -import retrofit.client.Response; - -import java.util.ArrayList; -import java.util.List; public class SearchActivity extends AppCompatActivity { - private final List citiesArray = new ArrayList(); - private Countries countries; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search); - - final SearchView s = (SearchView) findViewById(R.id.searchView); - ListView l = (ListView) findViewById(R.id.listView2); - - final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, citiesArray); - l.setAdapter(adapter); - - l.setOnItemClickListener(new AdapterView.OnItemClickListener() { - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - TextView t = (TextView) view; - s.setQuery(t.getText(), true); - } - }); - - s.setOnQueryTextListener(new SearchView.OnQueryTextListener() { - @Override - public boolean onQueryTextSubmit(String query) { - Intent intent = new Intent(SearchActivity.this, DetailsActivity.class); - intent.putExtra("city_name", query); - - startActivity(intent); - return false; - } - - @Override - public boolean onQueryTextChange(String newText) { - final boolean[] found = {false}; - - CitiesAdapters adapters = new CitiesAdapters(); - CitiesApi.AutocompleteApi s = adapters.getCitiesForAutocomplete(); - s.autocompletePlace(newText, Config.placesApiKey, new Callback() { - @Override - public void success(Predictions predictions, Response response) { - List cities = predictions.getCities(); - citiesArray.clear(); - found[0] = !cities.isEmpty(); - citiesArray.addAll(cities); - adapter.notifyDataSetChanged(); - } - - @Override - public void failure(RetrofitError error) { - Toast.makeText(getApplicationContext(), "Erreur lors de la récupération des pays", Toast.LENGTH_SHORT).show(); - } - }); - return found[0]; - } - }); } diff --git a/app/src/main/java/com/amine/myterio/app/fragments/SearchFragment.java b/app/src/main/java/com/amine/myterio/app/fragments/SearchFragment.java new file mode 100644 index 0000000..285ef7f --- /dev/null +++ b/app/src/main/java/com/amine/myterio/app/fragments/SearchFragment.java @@ -0,0 +1,89 @@ +package com.amine.myterio.app.fragments; + +import android.app.Activity; +import android.app.Fragment; +import android.content.Intent; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.*; +import com.amine.myterio.app.DetailsActivity; +import com.amine.myterio.app.R; +import com.amine.myterio.app.api.CitiesAdapters; +import com.amine.myterio.app.api.CitiesApi; +import com.amine.myterio.app.config.Config; +import com.amine.myterio.app.model.Countries; +import com.amine.myterio.app.model.Predictions; +import retrofit.Callback; +import retrofit.RetrofitError; +import retrofit.client.Response; + +import java.util.ArrayList; +import java.util.List; + +public class SearchFragment extends Fragment { + private final List citiesArray = new ArrayList(); + private Activity mActivity; + private Countries countries; + + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); + mActivity = activity; + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + View fragView = inflater.inflate(R.layout.search_fragment, null, false); + final SearchView s = (SearchView) fragView.findViewById(R.id.searchView); + ListView l = (ListView) fragView.findViewById(R.id.listView2); + + final ArrayAdapter adapter = new ArrayAdapter(mActivity, android.R.layout.simple_list_item_1, citiesArray); + l.setAdapter(adapter); + + l.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + TextView t = (TextView) view; + s.setQuery(t.getText(), true); + } + }); + + s.setOnQueryTextListener(new SearchView.OnQueryTextListener() { + @Override + public boolean onQueryTextSubmit(String query) { + Intent intent = new Intent(mActivity, DetailsActivity.class); + intent.putExtra("city_name", query); + + startActivity(intent); + return false; + } + + @Override + public boolean onQueryTextChange(String newText) { + final boolean[] found = {false}; + + CitiesAdapters adapters = new CitiesAdapters(); + CitiesApi.AutocompleteApi s = adapters.getCitiesForAutocomplete(); + s.autocompletePlace(newText, Config.placesApiKey, new Callback() { + @Override + public void success(Predictions predictions, Response response) { + List cities = predictions.getCities(); + citiesArray.clear(); + found[0] = !cities.isEmpty(); + citiesArray.addAll(cities); + adapter.notifyDataSetChanged(); + } + + @Override + public void failure(RetrofitError error) { + Toast.makeText(mActivity, R.string.country_list_error, Toast.LENGTH_SHORT).show(); + } + }); + return found[0]; + } + }); + return fragView; + } +} diff --git a/app/src/main/res/layout-large/activity_search.xml b/app/src/main/res/layout-large/activity_search.xml index 5591c63..8e9d28a 100644 --- a/app/src/main/res/layout-large/activity_search.xml +++ b/app/src/main/res/layout-large/activity_search.xml @@ -1,27 +1,15 @@ - - + - - - + diff --git a/app/src/main/res/layout-large/search_fragment.xml b/app/src/main/res/layout-large/search_fragment.xml new file mode 100644 index 0000000..5591c63 --- /dev/null +++ b/app/src/main/res/layout-large/search_fragment.xml @@ -0,0 +1,27 @@ + + + + + + + diff --git a/app/src/main/res/layout-normal/activity_search.xml b/app/src/main/res/layout-normal/activity_search.xml index b691300..8e9d28a 100644 --- a/app/src/main/res/layout-normal/activity_search.xml +++ b/app/src/main/res/layout-normal/activity_search.xml @@ -1,27 +1,15 @@ - - + - - - + diff --git a/app/src/main/res/layout-normal/search_fragment.xml b/app/src/main/res/layout-normal/search_fragment.xml new file mode 100644 index 0000000..b691300 --- /dev/null +++ b/app/src/main/res/layout-normal/search_fragment.xml @@ -0,0 +1,27 @@ + + + + + + + diff --git a/app/src/main/res/layout-small/activity_search.xml b/app/src/main/res/layout-small/activity_search.xml index b691300..8e9d28a 100644 --- a/app/src/main/res/layout-small/activity_search.xml +++ b/app/src/main/res/layout-small/activity_search.xml @@ -1,27 +1,15 @@ - - + - - - + diff --git a/app/src/main/res/layout-small/search_fragment.xml b/app/src/main/res/layout-small/search_fragment.xml new file mode 100644 index 0000000..b691300 --- /dev/null +++ b/app/src/main/res/layout-small/search_fragment.xml @@ -0,0 +1,27 @@ + + + + + + + diff --git a/app/src/main/res/layout-xlarge/activity_search.xml b/app/src/main/res/layout-xlarge/activity_search.xml index 5591c63..23964f2 100644 --- a/app/src/main/res/layout-xlarge/activity_search.xml +++ b/app/src/main/res/layout-xlarge/activity_search.xml @@ -2,26 +2,14 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - android:paddingBottom="@dimen/activity_vertical_margin" - android:paddingLeft="@dimen/activity_horizontal_margin" - android:paddingRight="@dimen/activity_horizontal_margin" - android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.amine.myterio.app.SearchActivity"> - - - + diff --git a/app/src/main/res/layout-xlarge/search_fragment.xml b/app/src/main/res/layout-xlarge/search_fragment.xml new file mode 100644 index 0000000..5591c63 --- /dev/null +++ b/app/src/main/res/layout-xlarge/search_fragment.xml @@ -0,0 +1,27 @@ + + + + + + + diff --git a/app/src/main/res/layout/activity_search.xml b/app/src/main/res/layout/activity_search.xml index b691300..4bfb364 100644 --- a/app/src/main/res/layout/activity_search.xml +++ b/app/src/main/res/layout/activity_search.xml @@ -2,10 +2,6 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - android:paddingBottom="@dimen/activity_vertical_margin" - android:paddingLeft="@dimen/activity_horizontal_margin" - android:paddingRight="@dimen/activity_horizontal_margin" - android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.amine.myterio.app.SearchActivity"> Delete Do you want to delete this location ? Delete the location ? + Error while getting the countires list diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e137b8b..f8ae9c0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -10,4 +10,5 @@ Supprimer la ville? Supprimer Annuler + Erreur lors de la récupération des pays