Reputation: 467
I need adapter data set to the auto complete text view in android .
Upvotes: 13
Views: 34535
Reputation: 332
Case: 1 (if array declare in string.xml file then )
string.xml
<string-array name="units_array">
<item>Bags</item>
<item>Box</item>
<item>Bottles</item>
<item>Cubic Meter</item>
<item>Cartons</item>
<item>Dozens</item>
<item>Grams</item>
<item>Kilograms</item>
<item>Kiloliter</item>
<item>Kilometre</item>
<item>Meters</item>
<item>Metric tons</item>
<item>Numbers</item>
<item>Packs</item>
<item>Pieces</item>
</string-array>
//MainActivity.Java
String[] UnitList = getResources().getStringArray(R.array.units_array);
ArrayAdapter<String> UnitAdapter = new ArrayAdapter<>(this, R.layout.drop_down_layout, UnitList);
UnitAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mBinding.autoCompleteTextViewUnit.setAdapter(UnitAdapter);
Case: 2 (if array declare in MainActivity then)
//MainActivity.Java
String[] array={"Bags","Box" ,"Bottles","Cubic Meter"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, array);
mBinding.autoCompleteTextViewUnit.setAdapter(adapter);
Case: 3 (if array is a API response then)
//MainActivity.Java
if (!response.getData().isEmpty()) {
List<ModelClass> responseGroupList = response.getData();
List<String> UnitList = new ArrayList<>();
for (ModelClass data : responseGroupList)
{
UnitList.add(data.getItemGroup());
}
ArrayAdapter<String> UnitAdapter = new ArrayAdapter<>(this, R.layout.drop_down_layout, R.id.textView, UnitList);
UnitAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mBinding.autoCompleteTextViewUnit.setAdapter(UnitAdapter);
}
default Select List Item position=0
mBinding.autoCompleteTextViewUnit.setText(UnitAdapter.getItem(0));
Upvotes: 0
Reputation: 34360
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);
Upvotes: 18
Reputation: 24476
Create one project for AutoCompleteTextView
and paste the code to required place -
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
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 TextView
Hope this helps you.
Upvotes: 5