I'm working on a project in which I have to access data from SQLite3 database and show suggestions to user while typing. I have tried this with AutoCompleteTextView, but it's not working. I'm working on this code:
ArrayList<String> sugestion = new ArrayList<String>();
public ViewGroup onCreateView (LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState )
{
myAutoComplete = (AutoCompleteTextView) group.findViewById(R.id.searchit);
searchDB = new searchAdapter(getActivity());
searchDB.getWritableDatabase();
myAutoComplete.addTextChangedListener(new TextWatcher(){
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
suggestion = searchDB.getSuggestion(s.toString());
Collections.sort(suggestion);
String[] item = new String[suggestion.size()];
item = suggestion.toArray(item);
for(String word : item)
System.out.println(word);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
searchAdapter = new ArrayAdapter<String>(getActivity() , android.R.layout.simple_dropdown_item_1line, item);
myAutoComplete.setAdapter(searchAdapter);
...
}
I have checked both the ArrayList
(from database) and String[]
array by printing it, they are having strings arranged.
My xml layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "@+id/group"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<AutoCompleteTextView
android:id="@+id/searchit"
android:layout_marginTop="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Choose The Country" >
<requestFocus />
</AutoCompleteTextView>
.....
</RelativeLayout>
Dropdown for string suggestion does not appear while typing. I have also tried with my own layout using textview instead of android.R.layout.simple_dropdown_item_1line
. What am I missing here?. Any help will be appreciated
Try this...
You have set the adapter only in
onCreate()
. But you have populated value for youritem
array only insideonTextChanged
inTextWatcher
. so, try to set theadapter
afetr populating youritem
array.