Android 如何将适配器设置为自动完成文本视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10613419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:22:52  来源:igfitidea点击:

How to set Adapter to Auto Complete Text view?

androidsearchautocompletetextview

提问by Rajender Reddy

I need adapter data set to the auto complete text view in android .

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

回答by Jawad Zeb

Create an array of String or get it from any function and create an ArrayAdapter of String then let the adapter to set the list for you .

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

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

回答by Praveenkumar

Create one project for AutoCompleteTextViewand paste the code to required place -

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

main.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

自动完成文本视图.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

}
}

Just use this example. And, figure out how they're setting adapter to AutoComplete TextViewHope this helps you.

就用这个例子。而且,弄清楚他们如何将适配器设置为AutoComplete TextView希望这对您有所帮助。