Skip to content Skip to sidebar Skip to footer

How To Set Adapter To Auto Complete Text View?

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

Solution 1:

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

Solution 2:

Create one project for AutoCompleteTextView and paste the code to required place -

main.xml

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

AutoCompleteTextview.java

publicclassAndroidAutoCompleteTextViewextendsActivityimplementsTextWatcher{

AutoCompleteTextView myAutoComplete;
String item[]={
  "January", "February", "March", "April",
  "May", "June", "July", "August",
  "September", "October", "November", "December"
};

   /** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

   myAutoComplete = (AutoCompleteTextView)findViewById(R.id.myautocomplete);

   myAutoComplete.addTextChangedListener(this);
   myAutoComplete.setAdapter(newArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, item));

   }

@OverridepublicvoidafterTextChanged(Editable arg0) {
 // TODO Auto-generated method stub

}

@OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count, int after) {
 // TODO Auto-generated method stub

}

@OverridepublicvoidonTextChanged(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 TextView Hope this helps you.

Post a Comment for "How To Set Adapter To Auto Complete Text View?"