1
I have a certain problem using the AutoCompleteTextView
android.
The AutoComplete
is presenting me duplicated data by searching both in the name and in the email. I would like it to present only one value, whether searching by contact name, or searching by email.
Basically, I have a list that stores a HashMap
containing the name and email of each contact:
List<Map<String, String>> contact_list = new ArrayList<>();
To fill the Hashmap
, I use a Cursor
to help me retrieve information from the database:
Cursor cursor = database.rawQuery(ContactTable.SELECT_ALL, null);
if(cursor.getCount() > 0){
while(cursor.moveToNext()){
Map<String, String> contact_info = new HashMap<>();
contact_info.put("name", cursor.getString(ContactTable.COLUMN_CONTACT_NAME_IDX));
contact_info.put("email", cursor.getString(ContactTable.COLUMN_CONTACT_EMAIL_IDX));
contact_list.add(contact_info);
}
}
After I add all the instances of HashMap
in my list, I add this list in a SimpleAdapter
and seventh this Adapter in my AutoCompleteTextView
:
auto_adapter = new SimpleAdapter(
mContext,
contact_list,
android.R.layout.simple_list_item_2,
new String[]{"name", "email"},
new int[]{android.R.id.text1, android.R.id.text2});
auto_complete.setAdapter(auto_adapter);
The threshold
has value "1" and was declared in layout file:
<AutoCompleteTextView
android:id="@+id/dialog_participant_name_auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:inputType="textCapWords"
android:hint="@string/dialog_participant_auto_name"
android:textColorHint="@color/hint_text_color"
android:completionThreshold="1"
android:visibility="gone"/>
It works almost perfectly, but it doubles the value of the search. For example, if I save a contact with the name "So-and-so" and the email "[email protected]", and search for "F", the AutoCompleteTextView
returns me two items:
Have you checked if you’re really not repeating in the database?! Are they mobile contacts or have you created a table using sqlite? Try using distinct
– viana
They are contacts that I added in my BD using sqlite, Even with DISTINCT the problem persists. I believe it’s because of the duplicate research itself
– Marcio Motta
At least in the code you’re showing here in your question, I haven’t found anything that might be duplicating. Maybe it is elsewhere in your code or the duplication is even in your bank. Usually the distinct uo always works.
– viana