【问题标题】:How to set Adapter to Auto Complete Text view?如何将适配器设置为自动完成文本视图?
【发布时间】:2012-05-23 17:07:25
【问题描述】:

我需要将适配器数据设置为 android 中的自动完成文本视图。

【问题讨论】:

    标签: android search autocompletetextview


    【解决方案1】:

    创建一个 String 数组或从任何函数中获取它并创建一个 String ArrayAdapter 然后让适配器为您设置列表。

     String[] array={"first","second item" ,"third item"};
     AutoCompleteTextView textView;
    
    ArrayAdapter<String> adapter;
    
    textView = (AutoCompleteTextView) findViewById(R.id.et_search);
    
    
    adapter = new ArrayAdapter<String>(PlayListActivity.this,
                            android.R.layout.simple_list_item_1, array);
    
                    textView.setAdapter(adapter);
    

    【讨论】:

      【解决方案2】:

      AutoCompleteTextView创建一个项目并将代码粘贴到所需位置-

      ma​​in.xml

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:orientation="vertical"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         >
      <TextView 
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="@string/hello"
         />
      <AutoCompleteTextView android:id="@+id/myautocomplete"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:completionThreshold="1"
      />
      </LinearLayout>
      

      AutoCompleteTextview.java

      public class AndroidAutoCompleteTextView extends Activity implements TextWatcher{
      
      AutoCompleteTextView myAutoComplete;
      String item[]={
        "January", "February", "March", "April",
        "May", "June", "July", "August",
        "September", "October", "November", "December"
      };
      
         /** Called when the activity is first created. */
         @Override
         public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
      
         myAutoComplete = (AutoCompleteTextView)findViewById(R.id.myautocomplete);
      
         myAutoComplete.addTextChangedListener(this);
         myAutoComplete.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, item));
      
         }
      
      @Override
      public void afterTextChanged(Editable arg0) {
       // TODO Auto-generated method stub
      
      }
      
      @Override
      public void beforeTextChanged(CharSequence s, int start, int count, int after) {
       // TODO Auto-generated method stub
      
      }
      
      @Override
      public void onTextChanged(CharSequence s, int start, int before, int count) {
       // TODO Auto-generated method stub
      
      }
      }
      

      只需使用此示例。并且,弄清楚他们如何将适配器设置为 AutoComplete TextView 希望这对您有所帮助。

      【讨论】:

        猜你喜欢
        • 2014-10-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多