Как заполнить AutoCompleteTextView из Cursor, используя SimpleCursorAdapter и загрузчик? - PullRequest
0 голосов
/ 04 апреля 2019

Вот мой код для заполнения AutoCompleteTextView из Курсора базы данных.

Однако, если это не считается хорошей практикой или необходимостью (?), Я хотел бы использовать Loader для предоставления Курсора.Возможно ли это?

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. 
}
...