2012-07-11 10 views
11

Zrobiłem AutoCompletetextView. Pozycje w menu rozwijanym AutoCompleteTextView nie są widoczne. Jak zmienić kolor tych przedmiotów.Pozycje z rozwijanej listy AutoCompleteTextView nie są widoczne. Jak zmienić ich kolor ...?

Jak to wygląda: - enter image description here

+0

Skąd wiesz, że otrzymujesz żadnych wyników automatycznego uzupełniania, może nie ma nic do pokazania? Czy próbowałeś LOGować wyniki i wiedzieć, że rzeczywiście istnieją jakieś wyniki? – theAlse

+0

dodaj również odpowiedni kod. –

+1

Tak, wyniki są dostępne. Sprawdziłem to. Rzeczywiście, jeśli kliknę na jakiś przedmiot, pojawi się. –

Odpowiedz

7

Podkreślam, że używając numeru android.R.layout.simple_dropdown_item_1line, pojawi się ten sam problem, który napotkano powyżej. Więc lepiej jest po prostu stworzyć własny plik TextView w pliku .xml.

14

Do sterowania sposób wyświetlania elementów w widoku autouzupełniania, trzeba ustawić textViewResourceId w adapterze. Możesz użyć ArrayAdapter i podać android.R.layout.simple_dropdown_item_1line jako textViewResourceId, jak pokazano poniżej.

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, yourList); 
AutoCompleteTextView autocompleteView = (AutoCompleteTextView) findViewById(R.id.autocomplete_box); 
autocompleteView.setAdapter(adapter); 

LUB

jeśli chcesz stworzyć swój własny styl na elementy wyświetlane, należy utworzyć XML z TextView jako element główny tak (pozwala mu nazwę my_custom_dropdown.xml z czarnego koloru tekstu i białym tle)

<?xml version="1.0" encoding="utf-8"?> 
<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="20sp" 
    android:padding="5sp" 
    android:textColor="@color/black" 
    android:background="@color/white"/> 

Następnie zapoznać się z xml w karty jak poniżej -

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.my_custom_dropdown, yourList); 
0

Po prostu użyj "android.R.layout.simple_list_item_1" zamiast "android.R.layout.simple_dropdown_item_1line" ..... Twój problem zostanie rozwiązany ...:)

+0

Ta odpowiedź nie działa w ogóle. Powinieneś go usunąć. – JuiCe

+0

Ta odpowiedź działa dla MultiAutoCompletetextView, a nie dla AutoCompletetextView – Cris

5

przypadku zmiany kodu z "android.R.layout.simple_list_item_1" do "android.R.layout.simple_dropdown_item_1line"nie działa dla ciebie

należy spróbować napisać ten kod przed setContentView

setTheme(android.R.style.Theme); 

on pracował dla mnie :)

+0

Czy określono motyw w manifeście? Ta praca jest również dostępna i możesz ją zastosować do całej aplikacji lub określonych działań. – Samuroid

0

Tworzę przykładowy projekt: AutoCompleteTextViewAdapter(Github Repo)

Trzeba realizować linię kontynuacji kodu źródłowego:

Aktywny

ArrayAdapter<String> myCustomAdapter = new ArrayAdapter<String>(this, R.layout.text_custom_view, countriesNames); 

final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.auto_complete_text_view); 
textView.setAdapter(myCustomAdapter); 

XML z niestandardowym TextView

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/country_name" 
    style="@style/CustomTextView" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:singleLine="true" /> 

ostateczny wynik

AutoCompleteTextViewAdapter

0

Here is answer in hope others will benefit

http://www.outofwhatbox.com/blog/2010/11/android-simpler-autocompletetextview-with-simplecursoradapter/

public class SelectState extends Activity { 

final static int[] to = new int[] { android.R.id.text1 }; 

    final static String[] from = new String[] { "state" }; 

    private TextView mStateCapitalView; 
    private AutoCompleteTextView mStateNameView; 
    private AutoCompleteDbAdapter mDbHelper; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mDbHelper = new AutoCompleteDbAdapter(this); 
     setContentView(R.layout.selectstate); 
     Button confirmButton = (Button) findViewById(R.id.confirm); 
     confirmButton.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       setResult(RESULT_OK); 
       finish(); 
      } 
     }); 

     mStateCapitalView = (TextView) findViewById(R.id.state_capital); 
     mStateNameView = (AutoCompleteTextView) findViewById(R.id.state_name); 

     // Create a SimpleCursorAdapter for the State Name field. 
     SimpleCursorAdapter adapter = 
      new SimpleCursorAdapter(this, 
        android.R.layout.simple_dropdown_item_1line, null, 
        from, to); 
     mStateNameView.setAdapter(adapter); 

     // Set an OnItemClickListener, to update dependent fields when 
     // a choice is made in the AutoCompleteTextView. 
     mStateNameView.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> listView, View view, 
         int position, long id) { 
       // Get the cursor, positioned to the corresponding row in the 
       // result set 
       Cursor cursor = (Cursor) listView.getItemAtPosition(position); 

       // Get the state's capital from this row in the database. 
       String capital = 
        cursor.getString(cursor.getColumnIndexOrThrow("capital")); 

       // Update the parent class's TextView 
       mStateCapitalView.setText(capital); 
      } 
     }); 

     // Set the CursorToStringConverter, to provide the labels for the 
     // choices to be displayed in the AutoCompleteTextView. 
     adapter.setCursorToStringConverter(new CursorToStringConverter() { 
      public String convertToString(android.database.Cursor cursor) { 
       // Get the label for this row out of the "state" column 
       final int columnIndex = cursor.getColumnIndexOrThrow("state"); 
       final String str = cursor.getString(columnIndex); 
       return str; 
      } 
     }); 

     // Set the FilterQueryProvider, to run queries for choices 
     // that match the specified input. 
     adapter.setFilterQueryProvider(new FilterQueryProvider() { 
      public Cursor runQuery(CharSequence constraint) { 
       // Search for states whose names begin with the specified letters. 
       Cursor cursor = mDbHelper.getMatchingStates(
         (constraint != null ? constraint.toString() : null)); 
       return cursor; 
      } 
     }); 
    } 
} 

Należy sprawdzić linii końcowe statyczne Int [] = nowy do Int [] {android.R.id.text1};

0

Oto najprostszy sposób. Utwórz własny plik XML XML. Na przykład custom_textview.xml

<?xml version="1.0" encoding="utf-8"?> 
    <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tv" 
    android:textColor="@color/font_content" 
    android:padding="5sp" 
    android:textSize="16sp" 
    android:layout_width="match_parent" 
    android:background="@drawable/any_background" 
    android:singleLine="true" 
    android:gravity="center" 
    android:layout_height="match_parent"/> 

potem w swojej działalności

//Create adapter object 
    ArrayAdapter<String> adpt = new ArrayAdapter<String>(context,R.layout.mytextview, what_ever_array_object_here); 
    searchAutoComplete.setAdapter(adpt); 
Powiązane problemy