开发者

Custom MultiAutoCompleteTextView like Autocomplete result must suggest me 'ThankYou' when i type 'TY'

开发者 https://www.devze.com 2023-04-05 20:48 出处:网络
I am creating demo of simple MultiAutoCompleteTextView. I have gone through the official docs MultiAutoCompleteTextView. Completed this simple tutorial.

I am creating demo of simple MultiAutoCompleteTextView.

I have gone through the official docs MultiAutoCompleteTextView. Completed this simple tutorial.

But this is something different than what i am trying to achieve.

What i want is :

When i type 'TY' then autocomplete result must suggest me 'Thank You'.

Such acronym and meaning开发者_StackOverflow社区 pairs are stored in the database.

Ofcourse my acronym and meaning pairs are not many so storage is not a problem(can store in String Array too).

How can i achieve this ?


My guess would be to do your own ArrayAdapter adapter. And in that adapter check what the string value is, if it is ie TY then add "Thank you".


Well I solved it by myselves :

I created Custom CursorAdapter. Using the object of my Custom CursorAdapter, i got desired results.

public static class MessageAdapter extends CursorAdapter implements Filterable 
    {
        DatabaseHelper dbHelper;

        public MessageAdapter(Context context, Cursor c) 
        {
            super(context, c);
            dbHelper=new DatabaseHelper(context);
            dbHelper.open();
            mContent = context.getContentResolver();
        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) 
        {
            final LayoutInflater inflater = LayoutInflater.from(context);
            final TextView view = (TextView) inflater.inflate(android.R.layout.simple_dropdown_item_1line, parent, false);
            view.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_MEANING)));
            return view;
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor)
        {
            ((TextView) view).setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_MEANING)));
        }

        @Override
        public String convertToString(Cursor cursor) {
            return cursor.getString(cursor.getColumnIndex(DatabaseHelper.KEY_MEANING));
        }

        @Override
        public Cursor runQueryOnBackgroundThread(CharSequence constraint)
        {
            if (getFilterQueryProvider() != null)
            {
                return getFilterQueryProvider().runQuery(constraint);
            }

            StringBuilder buffer = null;
            String args = null;
            if (constraint != null) 
            {
                args=constraint.toString();             
            }
            Cursor newCursor=dbHelper.getMeaning(args);
            //You have to return results based on the above cursor results. So query whatver you want and return this cursor back.
            return newCursor;
        }
        private ContentResolver mContent;        
    }

For Simplicity I have created getMeaning() method in my DatabaseHelper class which returns desired results based on String in MultiAutoCompleteTextView:

In DBHelper Class,

public Cursor getMeaning(String searchString)
    {
        Cursor cursor=sqdb.query(MY_DB, new String[]{KEY_ID,KEY_MEANING}, KEY_SEARCH+" like '"+searchString.toUpperCase()+"%'", null,null, null, null);
        return cursor;
    }

Reference : http://hello-android.blogspot.com/2011/06/using-autocompletetextview-with-sqlite.html


One way to achieve this would be to attach acronym and full strings something like Thank You - TY in the String array. This way autocomplete can recognize it when you type acronym too. Once the word is selected by the user, you can remove the part after hyphen '-'.

0

精彩评论

暂无评论...
验证码 换一张
取 消