search frag

This commit is contained in:
aminecmi 2015-07-12 17:47:59 +02:00
parent 550a3e8fce
commit 8340bbb48c
13 changed files with 234 additions and 151 deletions

View File

@ -1,82 +1,18 @@
package com.amine.myterio.app; package com.amine.myterio.app;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; 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 { public class SearchActivity extends AppCompatActivity {
private final List<String> citiesArray = new ArrayList<String>();
private Countries countries;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search); 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<Predictions>() {
@Override
public void success(Predictions predictions, Response response) {
List<String> 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];
}
});
} }

View File

@ -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<String> citiesArray = new ArrayList<String>();
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<Predictions>() {
@Override
public void success(Predictions predictions, Response response) {
List<String> 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;
}
}

View File

@ -1,27 +1,15 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="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"> tools:context="com.amine.myterio.app.SearchActivity">
<SearchView <fragment
android:id="@+id/searchView" android:id="@+id/search_frag"
android:layout_width="wrap_content" android:name="com.amine.myterio.app.fragments.SearchFragment"
android:layout_height="100dp" android:layout_width="0dp"
android:layout_alignParentEnd="true" android:layout_height="fill_parent"
android:layout_alignParentRight="true" android:layout_weight="1"
android:layout_alignParentTop="true"/> tools:layout="@layout/search_fragment"/>
<ListView </LinearLayout>
android:id="@+id/listView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/searchView"/>
</RelativeLayout>

View File

@ -0,0 +1,27 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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">
<SearchView
android:id="@+id/searchView"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"/>
<ListView
android:id="@+id/listView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/searchView"/>
</RelativeLayout>

View File

@ -1,27 +1,15 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="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"> tools:context="com.amine.myterio.app.SearchActivity">
<SearchView <fragment
android:id="@+id/searchView" android:id="@+id/search_frag"
android:layout_width="wrap_content" android:name="com.amine.myterio.app.fragments.SearchFragment"
android:layout_height="wrap_content" android:layout_width="0dp"
android:layout_alignParentEnd="true" android:layout_height="fill_parent"
android:layout_alignParentRight="true" android:layout_weight="1"
android:layout_alignParentTop="true"/> tools:layout="@layout/search_fragment"/>
<ListView </LinearLayout>
android:id="@+id/listView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/searchView"/>
</RelativeLayout>

View File

@ -0,0 +1,27 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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">
<SearchView
android:id="@+id/searchView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"/>
<ListView
android:id="@+id/listView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/searchView"/>
</RelativeLayout>

View File

@ -1,27 +1,15 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="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"> tools:context="com.amine.myterio.app.SearchActivity">
<SearchView <fragment
android:id="@+id/searchView" android:id="@+id/search_frag"
android:layout_width="wrap_content" android:name="com.amine.myterio.app.fragments.SearchFragment"
android:layout_height="wrap_content" android:layout_width="0dp"
android:layout_alignParentEnd="true" android:layout_height="fill_parent"
android:layout_alignParentRight="true" android:layout_weight="1"
android:layout_alignParentTop="true"/> tools:layout="@layout/search_fragment"/>
<ListView </LinearLayout>
android:id="@+id/listView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/searchView"/>
</RelativeLayout>

View File

@ -0,0 +1,27 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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">
<SearchView
android:id="@+id/searchView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"/>
<ListView
android:id="@+id/listView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/searchView"/>
</RelativeLayout>

View File

@ -2,26 +2,14 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="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"> tools:context="com.amine.myterio.app.SearchActivity">
<SearchView <fragment
android:id="@+id/searchView" android:id="@+id/search_frag"
android:layout_width="wrap_content" android:name="com.amine.myterio.app.fragments.SearchFragment"
android:layout_height="100dp" android:layout_width="0dp"
android:layout_alignParentEnd="true" android:layout_height="fill_parent"
android:layout_alignParentRight="true" android:layout_weight="1"
android:layout_alignParentTop="true"/> tools:layout="@layout/search_fragment"/>
<ListView
android:id="@+id/listView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/searchView"/>
</RelativeLayout> </RelativeLayout>

View File

@ -0,0 +1,27 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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">
<SearchView
android:id="@+id/searchView"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"/>
<ListView
android:id="@+id/listView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/searchView"/>
</RelativeLayout>

View File

@ -2,10 +2,6 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="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"> tools:context="com.amine.myterio.app.SearchActivity">
<SearchView <SearchView

View File

@ -10,4 +10,5 @@
<string name="delete">Delete</string> <string name="delete">Delete</string>
<string name="message_remove_from_favs">Do you want to delete this location ?</string> <string name="message_remove_from_favs">Do you want to delete this location ?</string>
<string name="title_remove_from_favs">Delete the location ?</string> <string name="title_remove_from_favs">Delete the location ?</string>
<string name="country_list_error">Error while getting the countires list</string>
</resources> </resources>

View File

@ -10,4 +10,5 @@
<string name="title_remove_from_favs">Supprimer la ville?</string> <string name="title_remove_from_favs">Supprimer la ville?</string>
<string name="delete">Supprimer</string> <string name="delete">Supprimer</string>
<string name="cancel">Annuler</string> <string name="cancel">Annuler</string>
<string name="country_list_error">Erreur lors de la récupération des pays</string>
</resources> </resources>