samedi 7 février 2015

Stop AutoCompleteTextView extending text on clicking suggestion


Vote count:

0




I have an AutoCompleteTextView that I filter every time a new letter is typed. The filtering process contains an AsyncTask since I am receiving data from an external database which takes a little time. This means after a new letter is typed - or deleted - the lists (array_name, array_id) are refreshed with the new data and that's what I show in the drop-down menu.


What happens by default? The filter runs, the AsyncTask queries the data I need, the lists are correctly filled and when I click on one of the suggestions it works just fine.


However, when I click on a suggestion the AutoCompleteTextView makes my code in my FilterProvider run AGAIN because it extends the text into the suggestion and it is considered a text change. This means that the AsyncTask will run again and delete the lists (array_name, array_id) instantly just before the click is executed. This only performed when I click on the suggestion before the second AsyncTask finishes. See:


Is it possible to somehow disable this functionality of the AutoCompleteTextView so that it doesn't extend the text when I click on the suggestion or do you have any other suggestions to solve this?



actv_name = (AutoCompleteTextView) findViewById(R.id.actv_name);
actv_name.setThreshold(3);
String[] from = { "name","bid" };
int[] to = { android.R.id.text1 };
a = new SimpleCursorAdapter(this, android.R.layout.simple_dropdown_item_1line, null, from, to, 0);
a.setStringConversionColumn(1);

FilterQueryProvider provider = new FilterQueryProvider() {

@Override
public Cursor runQuery(final CharSequence constraint) {

Log.d("TAG", "runQuery constraint: " + constraint);
if (constraint == null) {
return null;
}

array_name.clear();
array_id.clear();

try {

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = null;
httppost = new HttpPost(search_goal);

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("newtext", constraint.toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("error", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader( new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
//Log.i("sb", sb + "");
Bresult=sb.toString();
Log.i("Bresult", Bresult + "");
}catch(Exception e){
Log.e("error", "Error converting result "+e.toString());
}


c = new MatrixCursor(columnNames);

try {
if (Bresult != null) {
jArray = new JSONArray(Bresult);
for(int i=0;i<jArray.length();i++){
JSONArray innerJsonArray = jArray.getJSONArray(i);
for(int j=0;j<innerJsonArray.length();j++){
JSONObject jsonObject = innerJsonArray.getJSONObject(j);
array_name.add(jsonObject.getString("NAME"));
array_id.add(jsonObject.getString("ID"));
c.newRow().add(j).add(jsonObject.getString("NAME")).add(jsonObject.getString("ID"));
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}

for (int j=0; j<array_id.size(); j++) {
Log.d("ACTV LOG", array_id.get(j) + ", " + array_name.get(j));

}
Log.i("ASYNC C size", c.getCount() + "");

return c;
}
};

a.setFilterQueryProvider(provider);
actv_name.setAdapter(a);


OnClickListener:



actv_name.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick (AdapterView<?> parent, View view, int position, long id) {

Intent intent = new Intent(NewActivity.this, ProfileActivity.class);
intent.putExtra("id", array_id.get(position));
startActivity(intent);
actv_name.setText("");
}
});


asked 34 secs ago







Stop AutoCompleteTextView extending text on clicking suggestion

Aucun commentaire:

Enregistrer un commentaire