un problema que no puedo resolver.Android AutoCompleteTextView con datos de un servicio web, problemas al mostrar la lista de sugerencias
Lo que quiero lograr: Mostrar sugerencias en un AutoCompleteTextView procedente de una llamada al servicio web. El resultado final debería tener este aspecto:
Lo que hizo hasta ahora:
En mi diseño tengo una AutoCompleteTextView como esto
<AutoCompleteTextView
android:id="@+id/txtAutoSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:selectAllOnFocus="true"
android:singleLine="true" />
En mi Actividad tengo esto:
autoCompleteAdapter = new AdapterAutoComplete(context, ????? what should I have here?????, null);
autoCompleteAdapter.setNotifyOnChange(true);
txtAutoSearch.setAdapter(autoCompleteAdapter);
txtAutoSearch.addTextChangedListener(new TextWatcher() {
private boolean shouldAutoComplete = true;
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
shouldAutoComplete = true;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
if (shouldAutoComplete) {
new GetAutoSuggestionsTask().execute(s.toString());
}
}
});
El AsyncTask es bastante simple, onPostExecute
puse el adaptador con los datos devueltos por el servicio web
autoCompleteAdapter = new AdapterAutoComplete(context, ????? what should I have here?????,result);
txtAutoSearch.setAdapter(autoCompleteAdapter);
El adaptador es el siguiente:
public class AdapterAutoComplete extends ArrayAdapter<Person> {
private Activity activity;
private List<Person> lstPersons;
private static LayoutInflater inflater = null;
public AdapterAutoComplete(Activity a, int textViewResourceId, List<Person> lst) {
super(a, textViewResourceId, lst);
activity = a;
lstPersons = lst;
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return lstPersons.size();
}
public long getItemId(int position) {
return position;
}
public Person getCurrentPerson(int position) {
return lstPersons.get(position);
}
public void removeItem(int position) {
lstPersons.remove(position);
}
public static class ViewHolder {
public TextView txtName;
public TextView txtProfession;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if (convertView == null) {
vi = inflater.inflate(R.layout.people_list_item, null);
Utils.overrideFonts(activity, vi);
holder = new ViewHolder();
holder.txtName = (TextView) vi.findViewById(R.id.txtName);
holder.txtProfession = (TextView) vi.findViewById(R.id.txtProfession);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
Person o = lstPersons.get(position);
if (o.name != null) {
holder.txtName.setText(o.name);
} else {
holder.txtName.setText("N/A");
}
if (o.profession != null) {
holder.txtProfession.setText(o.profession);
} else {
holder.txtProfession.setText("N/A");
}
return vi;
}
}
Lo real sucede:
- el servicio web devuelve la lista que necesito, por lo que los datos están disponibles
- en Adapter el getView no parece ejecutarse, así que supongo que hay algo mal hecho en él.
- no hay una lista de sugerencias mostrada con mis valores
- No tengo idea de lo que necesito para el constructor del adaptador, para textViewResourceId. ¿Qué estoy haciendo mal? Estoy atascado por favor ayuda.