2012-07-11 32 views
11

Tôi đã tạo Chế độ xem Tự động hoàn thành. Các mục trong menu thả xuống của AutoCompleteTextView không hiển thị. Cách thay đổi màu của các mục đó.Các mục trong danh sách thả xuống của AutoCompleteTextView không hiển thị. Làm thế nào để thay đổi màu sắc của họ ..?

Đây là cách có vẻ: - enter image description here

+0

Làm thế nào để bạn biết rằng bạn đang nhận được bất kỳ kết quả tự động hoàn thành, có lẽ không có gì để hiển thị là gì? Bạn đã cố gắng LOG kết quả và biết rằng có thực sự tồn tại một số kết quả? – theAlse

+0

đăng một số mã có liên quan. –

+1

Có, kết quả là có. Tôi đã kiểm tra điều đó. Infact nếu tôi giữ bấm vào bất kỳ mục nào, nó xuất hiện. –

Trả lời

7

Chỉ cần chỉ ra rằng bằng cách sử dụng android.R.layout.simple_dropdown_item_1line nó sẽ cung cấp cho bạn cùng một vấn đề bạn gặp phải ở trên. Vì vậy, bạn tốt hơn hết chỉ cần tạo TextView của riêng bạn trong một tệp .xml.

14

Đối với việc kiểm soát cách bạn hiển thị các mục theo quan điểm autocomplete của bạn, bạn cần phải thiết lập các textViewResourceId trong bộ chuyển đổi của bạn. Bạn có thể sử dụng ArrayAdapter và cung cấp android.R.layout.simple_dropdown_item_1line làm textViewResourceId như được hiển thị bên dưới.

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

HOẶC

nếu bạn muốn tạo phong cách riêng của bạn cho các mục hiển thị, vui lòng tạo một XML với TextView như phần tử gốc như thế này (cho phép đặt tên là my_custom_dropdown.xml với văn bản màu đen và nền trắng)

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

sau đó tham khảo các xml trong adapter của bạn như sau -

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

Chỉ cần sử dụng "android.R.layout.simple_list_item_1" thay vì "android.R.layout.simple_dropdown_item_1line" ..... sự cố của bạn sẽ được giải quyết ...:)

+0

Câu trả lời này không hoạt động chút nào. Bạn nên loại bỏ nó. – JuiCe

+0

Câu trả lời này phù hợp với MultiAutoCompletetextView chứ không phải cho AutoCompletetextView – Cris

5

Nếu thay đổi mã từ "android.R.layout.simple_list_item_1" để "android.R.layout.simple_dropdown_item_1line"đã không làm việc cho bạn,

bạn nên cố gắng viết mã này trước setContentView

setTheme(android.R.style.Theme); 

Nó làm việc cho tôi :)

+0

Bạn có chỉ định chủ đề trong tệp kê khai của mình không? Công việc này là tốt và bạn có thể áp dụng nó cho toàn bộ ứng dụng hoặc các hoạt động cụ thể. – Samuroid

0

Tôi tạo một dự án mẫu: AutoCompleteTextViewAdapter(Github Repo)

.210

Bạn cần phải thực hiện dòng tiếp theo của mã nguồn:

Hoạt động

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 với Custom 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" /> 

kết quả cuối cùng

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

Vui lòng kiểm tra dòng thức static int [] để = new int [] {} android.R.id.text1;

0

Đây là cách đơn giản nhất. Tạo tập tin xml TextView của riêng bạn. Ví dụ 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"/> 

sau đó trong hoạt động của bạn

//Create adapter object 
    ArrayAdapter<String> adpt = new ArrayAdapter<String>(context,R.layout.mytextview, what_ever_array_object_here); 
    searchAutoComplete.setAdapter(adpt); 
Các vấn đề liên quan