P
P
Pavel2013-01-20 20:03:42
Android
Pavel, 2013-01-20 20:03:42

Android: AutoCompleteTextView + SimpleCursorAdapter. Data filtering, is it possible?

Good day.
The idea is this. There is a DB. Records from the database are displayed in the ListView. It is necessary to implement a "smart search" in the list, that is, you enter characters in a text field, a hint appears (a list of fields that matched the request). Decided to try AutoCompleteTextView.
in onCreate() activity create adapter and assign it to ListView and AutoCompleteTextView

...
        editQuery = (AutoCompleteTextView)this.findViewById(R.id.editQuery);
        
        listView = (ListView)this.findViewById(R.id.listView);

        // создааем адаптер и настраиваем список
        SimpleCursorAdapter scAdapter = new SimpleCursorAdapter(this, R.layout.item, cursor, from, to);
        scAdapter.setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() 
        {
            public CharSequence convertToString(Cursor cursor) 
            {
                return cursor.getString(NAME_COLUMN); // return name of desiase
            }
        });
        
        listView.setAdapter(scAdapter);
        editQuery.setAdapter(scAdapter);

        listView.setOnItemClickListener(this);
        editQuery.setOnItemClickListener(this); 
        ...

But, the problem is that the dropdown list from the AutoCompleteTextView shows everything that is in the ListView. And you only need what matches the characters entered in the AutoCompleteTextView.
How to do it? And what am I doing wrong.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
ara89, 2013-01-22
@ara89

As far as I understand, you need to update the cursor when entering text in the TextView. add a TextWatcher and when updating the text, update the cursor with a new query to the database

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question