Android : Providing auto autosuggestion in android places Api?
Posted
by
user1787493
on Stack Overflow
See other posts from Stack Overflow
or by user1787493
Published on 2012-11-02T04:52:08Z
Indexed on
2012/11/02
5:00 UTC
Read the original article
Hit count: 229
I am very new to android Google maps i write the following program for displaying the auto sugesstion in the android when i am type the text in the Autocomplete text box it is going the input to the url but the out put is not showing in the program .please see once and let me know where i am doing the mistake.
package com.example.exampleplaces;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.SyncStateContract.Constants;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.ProgressBar;
public class Place extends Activity {
private AutoCompleteTextView mAtv_DestinationLocaiton;
public ArrayList<String> autocompletePlaceList;
public boolean DestiClick2;
private ProgressBar destinationProgBar;
private static final String GOOGLE_PLACE_API_KEY = "";
private static final String GOOGLE_PLACE_AUTOCOMPLETE_URL = "https://maps.googleapis.com/maps/api/place/autocomplete/json?";
//https://maps.googleapis.com/maps/api/place/autocomplete/output?parameters
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
autocompletePlaceList = new ArrayList<String>();
destinationProgBar=(ProgressBar)findViewById(R.id.progressBar1);
mAtv_DestinationLocaiton = (AutoCompleteTextView) findViewById(R.id.et_govia_destination_location);
mAtv_DestinationLocaiton.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {
Log.i("Count", "" + count);
if (!mAtv_DestinationLocaiton.isPerformingCompletion()) {
autocompletePlaceList.clear();
DestiClick2 = false;
new loadDestinationDropList().execute(s.toString());
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
}
private class loadDestinationDropList extends
AsyncTask<String, Void, ArrayList<String>> {
@Override
protected void onPreExecute() {
// Showing progress dialog before sending http request
destinationProgBar.setVisibility(View.INVISIBLE);
}
protected ArrayList<String> doInBackground(String... unused) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
autocompletePlaceList = getAutocompletePlaces(mAtv_DestinationLocaiton.getText().toString());
return autocompletePlaceList;
}
public ArrayList<String> getAutocompletePlaces(String placeName) {
String response2 = "";
ArrayList<String> autocompletPlaceList = new ArrayList<String>();
String url = GOOGLE_PLACE_AUTOCOMPLETE_URL + "input="
+ placeName + "&sensor=false&key="
+ GOOGLE_PLACE_API_KEY;
Log.e("MyAutocompleteURL", "" + url);
try {
//response2 = httpCall.connectToGoogleServer(url);
JSONObject jsonObj = (JSONObject) new JSONTokener(response2.trim()
.toString()).nextValue();
JSONArray results = (JSONArray) jsonObj.getJSONArray("predictions");
for (int i = 0; i < results.length(); i++) {
Log.e("RESULTS",
"" + results.getJSONObject(i).getString("description"));
autocompletPlaceList.add(results.getJSONObject(i).getString(
"description"));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return autocompletPlaceList;
}
}
}
© Stack Overflow or respective owner