I have an AutoCompleteTextView
that dynamically updates the list of suggestions as the user types. My problem is that as I type the list gets updated but the dr
Seems like you really need to implement a filter:
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Filter;
import android.widget.TextView;
public class TestDoctor extends Activity {
TextView latText;
TextView lngText;
AutoCompleteTextView autoText;
ArrayAdapter adapter;
private Filter filter;
private static final int ADDRESS_TRESHOLD = 10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_doctor);
latText = (TextView) findViewById(R.id.latTextView);
lngText = (TextView) findViewById(R.id.longTextView);
autoText = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
Button button = (Button) findViewById(R.id.buttonDoctor);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
new AdapterUpdaterTask().execute();
}
});
autoText.setThreshold(ADDRESS_TRESHOLD);
autoText.setHint("Οδός Αριθμός, Περιοχή");
filter = new Filter() {
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
Log.i("Filter",
"Filter:" + constraint + " thread: " + Thread.currentThread());
if (constraint != null && constraint.length() > ADDRESS_TRESHOLD) {
Log.i("Filter", "doing a search ..");
new AdapterUpdaterTask().execute();
}
return null;
}
};
adapter = new ArrayAdapter(this,
android.R.layout.simple_dropdown_item_1line) {
public android.widget.Filter getFilter() {
return filter;
}
};
autoText.setAdapter(adapter);
adapter.setNotifyOnChange(false);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private List getAddressesFromText(String address) {
address = address.replace(' ', '+');
HttpHelper httpHelper = new HttpHelper();
InputStream inputStream = httpHelper
.makeHttpGetRequest("http://maps.google.com/maps/api/geocode/json?address="
+ address + "&sensor=false");
String response = httpHelper.inputStreamToString(inputStream);
List simpleAddresses = new ArrayList();
try {
JSONObject jsonObject = new JSONObject(response);
int size = ((JSONArray) jsonObject.get("results")).length();
for (int i = 0; i < size; i++) {
String formatted_address = ((JSONArray) jsonObject.get("results"))
.getJSONObject(i).getString("formatted_address");
Double lng = ((JSONArray) jsonObject.get("results")).getJSONObject(i)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lng");
Double lat = ((JSONArray) jsonObject.get("results")).getJSONObject(i)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lat");
simpleAddresses.add(i, new SimpleAddress(formatted_address, lat, lng));
}
return simpleAddresses;
} catch (JSONException e) {
return null;
}
}
public class AdapterUpdaterTask extends AsyncTask {
List simpleAddresses = new ArrayList();
@Override
protected Void doInBackground(Void... voids) {
Log.i("UPDATE", "1");
try {
simpleAddresses = getAddressesFromText(autoText.getText().toString());
} catch (NullPointerException e) {
}
Log.i("UPDATE", "2");
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
Log.i("UPDATE", "3");
int size = simpleAddresses.size();
if (size > 0) {
adapter.clear();
Log.i("ADAPTER_SIZE", "" + size);
for (int i = 0; i < size; i++) {
adapter.add(simpleAddresses.get(i).getFormatted_address());
Log.i("ADDED", simpleAddresses.get(i).getFormatted_address());
}
Log.i("UPDATE", "4");
adapter.notifyDataSetChanged();
autoText.showDropDown();
}
super.onPostExecute(aVoid);
}
}
}