如何使用SimpleCursorAdapter和加载程序从Cursor填充AutoCompleteTextView?

时间:2019-04-04 19:07:05

标签: android loader autocompletetextview simplecursoradapter

这是我的代码,用于从数据库Cursor填充AutoCompleteTextView。

但是,除非被认为不是很好的实践或必要的方法(?),否则我想使用装载程序来提供游标。有可能吗?

private static final Uri MY_CONTENT_PROVIDER_URI = ...;
private static final String MY_ID_COLUMN = "_id";
private static final String MY_TEXT_COLUMN = "my_col";

private SimpleCursorAdapter myCursorAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    myCursorAdapter = new SimpleCursorAdapter(
            mActivity, // context
            android.R.layout.simple_list_item_1, // layout
            null, // cursor
            new String[] {
                    MY_TEXT_COLUMN
            }, // from
            new int[] {
                    android.R.id.text1
            }, // to
            0 // flags
    );

    // This provides the labels to be displayed in the AutoCompleteTextView.
    myCursorAdapter.setCursorToStringConverter(new SimpleCursorAdapter.CursorToStringConverter() {
        public String convertToString(Cursor cursor) {
            // Get the label for this row from our text column
            return cursor.getString(cursor.getColumnIndexOrThrow(MY_TEXT_COLUMN));
        }
    });

    // This performs the query.
    myCursorAdapter.setFilterQueryProvider(new FilterQueryProvider() {
        public Cursor runQuery(CharSequence constraint) {

            String selection;
            if (constraint == null || TextUtils.isEmpty(constraint.toString().trim())) {
                selection = null;
            }
            else {
                String constraintString = constraint.toString();

                // Entire text starts with...
                selection = MY_TEXT_COLUMN + " LIKE '" + constraintString + "%'";

                selection += " OR ";

                // Any word within the entire text starts with...
                selection += MY_TEXT_COLUMN + " LIKE '% " + constraintString + "%'";
            }

            return mActivity.getContentResolver().query(
                    MY_CONTENT_PROVIDER_URI,
                    new String[] {
                            MY_ID_COLUMN,
                            MY_TEXT_COLUMN
                    },
                    selection,
                    null,
                    null);
        }
    });

}


@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_my, container, false);

    myAutoCompleteTextView = view.findViewById(R.id.myAutoCompleteTextView);
    myAutoCompleteTextView.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            // User entered new text OR clicked a suggestion

            handleUserInput(s.toString, -1);

            // If user clicked a suggestion (rather than entering new text), the setOnItemClickListener callback will also be called.
        }

        @Override
        public void afterTextChanged(Editable s) {}

    });
    myAutoCompleteTextView.setOnItemClickListener((AdapterView<?> listView, View arg1, int pos, long rowId) -> {

        // User clicked a suggestion

        Cursor cursor = (Cursor) listView.getItemAtPosition(pos);
        String myText = cursor.getString(cursor.getColumnIndexOrThrow(MY_TEXT_COLUMN));
        //long myId = cursor.getString(cursor.getColumnIndexOrThrow(MY_ID_COLUMN)); // Same as rowId

        handleUserInput(myText, rowId);

    });
    myAutoCompleteTextView.setAdapter(myCursorAdapter);

}

private void handleUserInput(String text, long rowId) {
    // TODO - Process the new text. 
}

0 个答案:

没有答案