Выделите искомый текст в массиве AdAdter ListView - PullRequest
2 голосов
/ 24 сентября 2019

Можно ли выделить искомый текст, который я использую arrayAdapter ListView

Вот мой код

  private TextWatcher filterTextWatcher = new TextWatcher() {

    public void afterTextChanged(Editable s) {
    }

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

    public void onTextChanged(CharSequence s, int start, int before,
                              int count) {
        adapter.getFilter().filter(s);

    }
};

 protected void onPostExecute(final List<Site> sites) {
        progressBar.hide();
        adapter = new ArrayAdapter(context, R.layout.alert_dialog_single_choice_list_item,sites);
        list.setAdapter(adapter);
        list.setItemChecked(selectedSite, true);
        list.setSelection(selectedSite);
        filterText.setVisibility(View.VISIBLE);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() 

    }

Это метод onViewCreated

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    filterText = (EditText) view.findViewById(R.id.et_search_state);
    filterText.addTextChangedListener(filterTextWatcher);
    list = (ListView) view.findViewById(R.id.lv_state);
    progressBar = view.findViewById(R.id.progressBar);
    final ImageButton btnClear = view.findViewById(R.id.btn_clear);
    btnClear.setOnClickListener(this);
    new BindDataAsync().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    filterText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (charSequence.length() > 0) {
                btnClear.setVisibility(View.VISIBLE);
            } else {
                btnClear.setVisibility(View.INVISIBLE);
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });

}

У меня есть проверка это , но здесь они используют пользовательские adapter;

1 Ответ

1 голос
/ 27 сентября 2019

Попробуйте ниже:

  1. Добавить глобальное поле, String mSearchText = "";

  2. Перед вызовом фильтра (adapter.getFilter().filter(s);), добавьте mSearchText = s.toString();

  3. Изменить

adapter = new ArrayAdapter(context, R.layout.alert_dialog_single_choice_list_item,sites);

на

    adapter = new ArrayAdapter(context, R.layout.alert_dialog_single_choice_list_item,sites){
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView text;
            text = (TextView)super.getView(position, convertView, parent);
            String fullText = getItem(position);
            // highlight search text
            if (mSearchText != null && !mSearchText.isEmpty()) {
                int startPos = fullText.toLowerCase(Locale.US).indexOf(mSearchText.toLowerCase(Locale.US));
                int endPos = startPos + mSearchText.length();
                if (startPos != -1) {
                    Spannable spannable = new SpannableString(fullText);
                    ColorStateList blueColor = new ColorStateList(new int[][]{new int[]{}}, new int[]{Color.BLUE});
                    TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, blueColor, null);
                    spannable.setSpan(highlightSpan, startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    text.setText(spannable);
                } else {
                    text.setText(fullText);
                }
            } else {
                text.setText(fullText);
            }
            return text;
        }
    };

Надеюсь, что поможет!

...