2012-05-25 18 views
5

Moje XML:AutoCompleteTextView nie pokazuje żadnych rozwijanych elementów

<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" /> 

mojego kodu 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); 

To nie działa atall .... mam na myśli jej po prostu działa jak EditTextView. Gdzie się mylę?

kompletny kod:

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

Nie działa w ogóle? Ile znaków wpisałeś, aby sprawdzić? –

+4

Użyłeś Próg jako 2, więc wystrzeli po wpisaniu 2 znaków – Venky

+0

tak, piszę więcej niż 2 zawsze ... nie pokazuje żadnych rozwijanych pozycji – Housefly

Odpowiedz

11

Widziałem tylko drugą pytanie przed obejrzeniu tego. Przez jakiś czas zmagałem się z autouzupełnianiem i prawie wróciłem do twojej nowej implementacji pobierania wszystkich słów kluczowych, aż w końcu udało mi się to zrobić. To, co zrobiłem, było;

//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); 

W metodzie onTextChanged mojego textwatcher jest, otrzymuję sugeruje używając asynctask

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

W latach AsyncTask za onPostExecute I następnie zaktualizować 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(); 

zobaczyć setNotifyOnChange i notifyDataSetChanged więcej informacji

+8

Wygląda na to, że nie zrozumiałeś w pełni pojęcia metody notifyDataSetChanged(). Tworzysz nową instancję ArrayAdapter w swojej metodzie onPostExecute i ustawiasz ją jako adapter. Wywołanie metody notifyDataSetChanged() w twoim przykładzie jest bezużyteczne. –

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); 

jego pracy tylko komentować et.addtext linii ...

+0

tak próbowałem, że nawet .... to nie działało dla mnie – Housefly

+0

jego działa w mojej wersji Androida 2.3.1 idealnie .. – jigspatel

+0

czy wersja ma znaczenie dla autocompletetextview ??? ... nie wiem ... mam mniejsze api ... muszę sprawdzić, zmieniając – Housefly

1

to fragment z mojego projektu. Myślę, że po otrzymaniu danych z usług, wszystko co musisz zrobić, to:

  1. wyczyścić poprzednie dane.
  2. wyczyść poprzednie wartości adaptera.
  3. następnie dodaj wartości do listy danych za pomocą metody add() lub addAll().
  4. powiadamia zmienione dane przez wywołanie metody notifyDataSetChanged() na adapterze.

    @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(); 
    

    }

ale przed tym wszystkim upewnij się, że został dołączony adapter do pełnego auto TextView jeśli nie to zrobić w następujący sposób:

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

completeTextViewPatient.setAdapter(stringArrayAdapterPatient); 
Powiązane problemy