Reputation: 1683
I have a AutoCompleteTextView in my layout. When a user enter "@" character in that i have to show them some suggestions. It normally names i get it from internet.
I am getting the names and i create an ArrayAdapter as shown below.
autoCtextView.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
String lsatChar = s.toString().substring(s.length()-1,s.length());
if(lsatChar.equals("@")) {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(DisplayQuestionDetails.this,
android.R.layout.simple_list_item_1, namesLsist);
autoCtextView.setAdapter(adapter);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
But the suggestions are not shown. Am i doing anything wrong ? Please ask if need clarification on question
Upvotes: 2
Views: 10949
Reputation: 31
In my fragment layout, if I don't use the ems attribute (field size in number of "m"s), the suggestions will not be shown. When it is present, the suggestions are shown.
<AutoCompleteTextView
android:id="@+id/enter_activities_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:ems="10"
android:completionThreshold="1"/>
Upvotes: 3
Reputation: 370
Do you miss autoCtextView.setThreshold(1);
?
(to start working from first character)
for example demo:
String[] strList={"a","aaa","aabb","b","bbc","cbb","c","cdd","caa","d","ddc","dda","e","eea","ebc","aec"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Creating the instance of ArrayAdapter containing list
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this,android.R.layout.select_dialog_item,strList);
//Getting the instance of AutoCompleteTextView
AutoCompleteTextView autoCtextView= (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
autoCtextView.setThreshold(1); //to start working from first character
autoCtextView.setAdapter(adapter);//set the adapter data to the AutoCompleteTextView
}
Upvotes: 6
Reputation: 5302
I made custom AutoComplete Textbox with the help of this blog. It works when user types "@something", e.g."@f" then all the names which matches 'f' will come up.
CustomAutoComplete.java
public class CustomAutoComplete extends AutoCompleteTextView {
private String previous = "";
private String seperator = "@";
public CustomAutoComplete(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
this.setThreshold(0);
}
public CustomAutoComplete(final Context context, final AttributeSet attrs) {
super(context, attrs);
this.setThreshold(0);
}
public CustomAutoComplete(final Context context) {
super(context);
this.setThreshold(0);
}
/**
* This method filters out the existing text till the separator
* and launched the filtering process again
*/
@Override
protected void performFiltering(final CharSequence text, final int keyCode) {
String filterText = text.toString().trim();
previous = filterText.substring(0, filterText.lastIndexOf(getSeperator()) + 1);
filterText = filterText.substring(filterText.lastIndexOf(getSeperator()) + 1);
if (!TextUtils.isEmpty(filterText)) {
super.performFiltering(filterText, keyCode);
}
}
/**
* After a selection, capture the new value and append to the existing
* text
*/
@Override
protected void replaceText(final CharSequence text) {
super.replaceText(previous + text + getSeperator());
}
public String getSeperator() {
return seperator;
}
}
Layout file:
<com.example.app.CustomAutoComplete
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/autoCompleteTextView"
android:layout_gravity="center_horizontal|top" />
MainActivity:
public class MainActivity extends ActionBarActivity {
CustomAutoComplete autoCompleteTextView;
String[] namesLsist = {"zivame","ziooo","zoooO","flipme","flipkart"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
autoCompleteTextView = (CustomAutoComplete) findViewById(R.id.autoCompleteTextView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, namesLsist);
autoCompleteTextView.setAdapter(adapter);
}
}
Upvotes: 0
Reputation: 814
After declare autocompleteTextView than fill the first adapter.
like Ref here
public class CountriesActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.countries_list);
textView.setAdapter(adapter);
}
private static final String[] COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain"
};
}
Upvotes: 3
Reputation: 8134
Do this:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(DisplayQuestionDetails.this, android.R.layout.simple_list_item_1, namesLsist);
autoCtextView.setAdapter(adapter);
Before autoCtextView.addTextChangedListener(new TextWatcher() {...
Upvotes: 1