I use google autocomplete suggestions in my application. It works fine, but I want to improve its performance. When the user collects a place, he gives suggestions after a long delay or sometimes after deleting the last characters. How to improve performance? Please help me. thanks in advance
Here is my code
public class invoice extends Activity
{
AutoCompleteTextView edit_destination;
DownloadTask placesDownloadTask;
DownloadTask placeDetailsDownloadTask;
ParserTask placesParserTask;
ParserTask placeDetailsParserTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.create_invoice_activity);
edit_destination=(AutoCompleteTextView) findViewById(R.id.destination);
edit_destination.setThreshold(1);
edit_destination.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
placesDownloadTask = new DownloadTask(PLACES);
String url = getAutoCompleteUrl(s.toString());
placesDownloadTask.execute(url);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
edit_destination.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int index,
long id) {
ListView lv = (ListView) arg0;
SimpleAdapter adapter = (SimpleAdapter) arg0.getAdapter();
HashMap<String, String> hm = (HashMap<String, String>) adapter.getItem(index);
selected_place=hm.get("description");
placeDetailsDownloadTask = new DownloadTask(PLACES_DETAILS);
String url = getPlaceDetailsUrl(hm.get("reference"));
placeDetailsDownloadTask.execute(url);
}
});
}
private String getAutoCompleteUrl(String place){
String key = "YOUR KEY";
String input = "input="+place;
String types = "types=geocode";
String sensor = "sensor=false";
String parameters = input+"&"+types+"&"+sensor+"&"+key;
String output = "json";
String url = "https://maps.googleapis.com/maps/api/place/autocomplete/"+output+"?"+parameters;
return url;
}
private String getPlaceDetailsUrl(String ref){
String key = "YOUR KEY";
String reference = "reference="+ref;
String sensor = "sensor=false";
String parameters = reference+"&"+sensor+"&"+key;
String output = "json";
String url = "https://maps.googleapis.com/maps/api/place/details/"+output+"?"+parameters;
return url;
}
private String downloadUrl(String strUrl) throws IOException{
String data = "";
InputStream iStream = null;
HttpURLConnection urlConnection = null;
try{
URL url = new URL(strUrl);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuffer sb = new StringBuffer();
String line = "";
while( ( line = br.readLine()) != null){
sb.append(line);
}
data = sb.toString();
br.close();
}catch(Exception e){
Log.d("Exception while downloading url", e.toString());
}finally{
iStream.close();
urlConnection.disconnect();
}
return data;
}
private class DownloadTask extends AsyncTask<String, Void, String>{
private int downloadType=0;
public DownloadTask(int type){
this.downloadType = type;
}
@Override
protected String doInBackground(String... url) {
String data = "";
try{
data = downloadUrl(url[0]);
}catch(Exception e){
Log.d("Background Task",e.toString());
}
return data;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
switch(downloadType){
case PLACES:
placesParserTask = new ParserTask(PLACES);
System.out.println(result);
placesParserTask.execute(result);
break;
case PLACES_DETAILS :
placeDetailsParserTask = new ParserTask(PLACES_DETAILS);
placeDetailsParserTask.execute(result);
}
}
}
private class ParserTask extends AsyncTask<String, Integer, List<HashMap<String,String>>>{
int parserType = 0;
public ParserTask(int type){
this.parserType = type;
}
@Override
protected List<HashMap<String, String>> doInBackground(String... jsonData) {
JSONObject jObject;
List<HashMap<String, String>> list = null;
try{
jObject = new JSONObject(jsonData[0]);
switch(parserType){
case PLACES :
PlaceJSONParser placeJsonParser = new PlaceJSONParser();
list = placeJsonParser.parse(jObject);
break;
case PLACES_DETAILS :
PlaceDetailsJSONParser placeDetailsJsonParser = new PlaceDetailsJSONParser();
list = placeDetailsJsonParser.parse(jObject);
}
}catch(Exception e){
Log.d("Exception",e.toString());
}
return list;
}
@Override
protected void onPostExecute(List<HashMap<String, String>> result) {
switch(parserType){
case PLACES :
String[] from = new String[] { "description"};
int[] to = new int[] { android.R.id.text1 };
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), result, android.R.layout.simple_list_item_1, from, to);
edit_destination.setAdapter(adapter);
break;
case PLACES_DETAILS :
HashMap<String, String> hm = result.get(0);
latitude = Double.parseDouble(hm.get("lat"));
System.out.println(latitude);
longitude = Double.parseDouble(hm.get("lng"));
System.out.println(longitude);
Toast.makeText(invoice.this, latitude+","+longitude , Toast.LENGTH_LONG).show();
SharedPreferences pref=getSharedPreferences("LOC", 0);
String S_lat,S_long;
S_lat=pref.getString("LAT", "");
S_long= pref.getString("LONG","");
source_lat=Double.parseDouble(S_lat);
source_long=Double.parseDouble(S_long);
break;
}
}
}
source
share