I want to develop a dynamic AutoCompleteTextView
for Android that populate with JSON
data retrieved from MySQL database. Basically it is not a hard task but where I am facing problem? I am inspired by the jQuery Autocomplete
option where dynamically fetch data after input letters. But android AutoCompleteTextView
is pre-populated with all JSON
data.
I am trying to query from millions of data which is really hard to store. Is there any way to search database for the input dynamically?
Eg:
Such as if user input "a" then it will retrieve the best result for "a" . Then if user type "ab" it will be refreshed and populated with new results from database.
Thanks
I'll give you just a general overview only for your task Which looks like this,
public List<String> suggest; //List of suggestions
autoComplete.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable editable) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
String newText = s.toString();
new getJson().execute(newText);
}
});
getJson AsyncTask -> For retrieving new values from server
class getJson extends AsyncTask<String,String,String>{
@Override
protected String doInBackground(String... key) {
String newText = key[0];
newText = newText.trim();
newText = newText.replace(" ", "+");
try{
//Codes to retrieve the data for suggestions
suggest = new ArrayList<String>();
JSONArray jArray = new JSONArray(data);
for(loop the array){
String SuggestKey = //retrieve values by iterating;
suggest.add(SuggestKey);
}
}catch(Exception e){
Log.w("Error", e.getMessage());
}
//Populate suggestions
runOnUiThread(new Runnable(){
public void run(){
aAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.item,suggest);
autoComplete.setAdapter(aAdapter);
aAdapter.notifyDataSetChanged();
}
});
return null;
}
See here for the detail info.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.