2012-05-25 12 views
5

Mi XML:AutoCompleteTextView no mostrar ningún desplegables artículos

<AutoCompleteTextView 
     android:id="@+id/searchAutoCompleteTextView_feed" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:clickable="true" 
     android:completionThreshold="2" 
     android:hint="@string/search" /> 

mi código java:

AutoCompleteTextView eT = (AutoCompleteTextView)findViewById(R.id.searchAutoCompleteTextView_feed); 
eT.addTextChangedListener(this); 
String[] sa = new String[]{"apple", "mango", "banana", "apple mango", "mango banana"}; 
ArrayAdapter<String> aAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, sa); 
eT.setAdapter(aAdapter); 

Esto no está funcionando atall .... me refiero a su trabajo como un solo EditTextView. ¿Dónde estoy equivocado?

código completo:

public class FeedListViewActivity extends ListActivity implements TextWatcher{ 


    private AutoCompleteTextView eT; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.feed); 

     eT = (AutoCompleteTextView) findViewById(R.id.searchAutoCompleteTextView_feed); 
     eT.addTextChangedListener(this); 

        Thread thread = new Thread(null, loadMoreListItems); 
        thread.start(); 
    } 

    private Runnable returnRes = new Runnable() { 
     public void run() { 

      //code for other purposes 
     } 
    }; 

    private Runnable loadMoreListItems = new Runnable() { 
     public void run() { 
      getProductNames(); 

      // Done! now continue on the UI thread 
      runOnUiThread(returnRes); 
     } 
    }; 

    protected void getProductNames() { 

      String[] sa = new String[]{"apple", "mango", "banana", "apple mango", "mango banana"}; 

      ArrayAdapter<String> aAdapter = new ArrayAdapter<String>(getApplicationContext(), 
        android.R.layout.simple_dropdown_item_1line, sa); 
      eT.setAdapter(aAdapter); 

    } 

    public void afterTextChanged(Editable s) { 
     // 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) { 
     // TODO Auto-generated method stub 

    } 
} 
+0

No funciona en absoluto? ¿Cuántos caracteres escribiste para verificar? –

+4

Usó Umbral como 2, por lo que se activará después de escribir 2 caracteres – Venky

+0

Sí, estoy escribiendo más de 2 siempre ... no muestra ningún elemento desplegable – Housefly

Respuesta

11

Acabo de ver su otra pregunta antes de ver a éste. Estuve luchando con el autocompletado por un tiempo y casi volví a su nueva implementación de descargar todas las palabras clave hasta que finalmente lo conseguí. Lo que hice fue;

//In the onCreate 
//The suggestArray is just a static array with a few keywords 
this.suggestAdapter = new ArrayAdapter<String>(this, this.suggestionsView, suggestArray); 
//The setNotifyOnChange informs all views attached to the adapter to update themselves 
//if the adapter is changed 
this.suggestAdapter.setNotifyOnChange(true); 

En el método OnTextChanged de mi textwatcher, consigo el sugiere el uso de un AsyncTask

//suggestsThread is an AsyncTask object 
suggestsThread.cancel(true); 
suggestsThread = new WertAgentThread(); 
suggestsThread.execute(s.toString()); 

En onPostExecute del AsyncTask entonces puedo actualizar el AutoCompleteTextView

//suggestions is the result of the http request with the suggestions 
this.suggestAdapter = new ArrayAdapter<String>(this, R.layout.suggestions, suggestions); 
this.suggestions.setAdapter(this.suggestAdapter); 
//notifydatasetchanged forces the dropdown to be shown. 
this.suggestAdapter.notifyDataSetChanged(); 

Ver setNotifyOnChange y notifyDataSetChanged para más información

+8

Parece que no has entendido completamente el concepto del método notifyDataSetChanged(). Está creando una nueva instancia de ArrayAdapter en su método onPostExecute y lo configura como el adaptador. La llamada al método notifyDataSetChanged() es inútil en su ejemplo. –

0
AutoCompleteTextView eT = (AutoCompleteTextView)findViewById(R.id.searchAutoCompleteTextView_feed); 
// eT.addTextChangedListener(this); 
    String[] sa = new String[]{"apple", "mango", "banana", "apple mango", "mango banana"}; 
    ArrayAdapter<String> aAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, sa); 
    eT.setAdapter(aAdapter); 

su trabajo acaba de comentar en la línea et.addtext ...

+0

Sí, lo intenté incluso ... no funcionó para mí – Housefly

+0

funciona perfectamente en mi versión de Android 2.3.1 .. – jigspatel

+0

hace la versión es importante para autocompletetextview ??? ... no sé ... soy un api más bajo ... necesito verificar cambiando – Housefly

1

esto es un fragmento de mi proyecto. Creo que después de obtener los datos de los servicios todo lo que tiene que hacer es:

  1. borre sus datos previos.
  2. borra los valores del adaptador anterior.
  3. luego agregue valores a su lista de datos usando el método add() o addAll().
  4. notifique los datos modificados llamando a notifyDataSetChanged() en el adaptador.

    @Override 
    public void onGetPatient(List<PatientSearchModel> patientSearchModelList) { 
    
    //here we got the raw data traverse it to get the filtered names data for the suggestions 
    
    stringArrayListPatients.clear(); 
    stringArrayAdapterPatient.clear(); 
    for (PatientSearchModel patientSearchModel:patientSearchModelList){ 
    
        if (patientSearchModel.getFullName()!=null){ 
    
         stringArrayListPatients.add(patientSearchModel.getFullName()); 
    
        } 
    
    } 
    
    //update the array adapter for patient search 
    stringArrayAdapterPatient.addAll(stringArrayListPatients); 
    stringArrayAdapterPatient.notifyDataSetChanged(); 
    

    }

pero antes de todo esto, asegúrese de que ha conectado el adaptador a la auto completo TextView si no lo hacen de la siguiente manera:

ArrayAdapter<String> stringArrayAdapterPatient= new ArrayAdapter<String>(getActivity(),android.support.v7.appcompat.R.layout.select_dialog_item_material,stringArrayListPatients); 

completeTextViewPatient.setAdapter(stringArrayAdapterPatient); 
Cuestiones relacionadas