Android: как очистить цвет Spannable в Recyclerview с помощью ViewModel? - PullRequest
0 голосов
/ 24 октября 2019

Приведенный ниже код адаптера для RecyclerView успешно выделяет введенный пользователем текст (используя строку EditText, связанную с SearchView) в зеленый цвет.

public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {

    String cardNumsHighlight = String.valueOf(card.getId());
    String todoHighlight = card.getTodo().toLowerCase(Locale.getDefault());
    String note1Highlight = card.getNote1().toLowerCase(Locale.getDefault());

    // Set up the code for the highlighted text of successful search results
    int cardNumsStartPos = cardNumsHighlight.indexOf(searchString);
    int cardNumsEndPos = cardNumsStartPos + searchString.length();
    int todoStartPos = todoHighlight.indexOf(searchString);
    int todoEndPos = todoStartPos + searchString.length();
    int note1StartPos = note1Highlight.indexOf(searchString);
    int note1EndPos = note1StartPos + searchString.length();

    Spannable spanString1 = Spannable.Factory.getInstance().newSpannable(
                itemHolder.cardBlankTextNumstotal.getText());
    Spannable spanString2 = Spannable.Factory.getInstance().newSpannable(
                itemHolder.cardBlankText2.getText());
    Spannable spanString4 = Spannable.Factory.getInstance().newSpannable(
                itemHolder.cardBlankText4.getText());

    if (cardNumsStartPos != -1 && searchString.length() > 0 && cardNumsHighlight.contains(searchString)) {
            spanString1.setSpan(new ForegroundColorSpan(Color.GREEN), cardNumsStartPos, cardNumsEndPos,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            itemHolder.cardBlankTextNumstotal.setText(spanString1);
        }
    if (todoStartPos != -1 && searchString.length() > 0 && todoHighlight.contains(searchString)) {
            spanString2.setSpan(new ForegroundColorSpan(Color.GREEN), todoStartPos, todoEndPos,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            itemHolder.cardBlankText2.setText(spanString2);
        }
    if (note1StartPos != -1 && searchString.length() > 0 && note1Highlight.contains(searchString)) {
            spanString4.setSpan(new ForegroundColorSpan(Color.GREEN), note1StartPos, note1EndPos,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            itemHolder.cardBlankText4.setText(spanString4);
        }  

Я попытался добавить следующее код, используя removeSpan () вОтвет здесь: Android Spannable: как очистить цвет? очистить цвет, когда запрос не возвращает никаких элементов, но безуспешно. Что мне здесь не хватает?

Activity
...
@Override
public void afterTextChanged(Editable s) {
    if (s.toString().length() >0) {
        String queryText = "%" + s.toString() + "%";

  mQuickcardViewModel.searchQuery(queryText).observe(MainActivity.this, searchQuickcards -> {

   searchList = searchQuickcards;  

   if (!mSearchView.isIconified() && searchList.size() == 0) {

       mQuickcardViewModel.loadFullList();
   }
   ...


ViewModel
...
public LiveData<List<Quickcard>> getAllCards() {
    if (quickcards == null) {
        quickcards = new MutableLiveData<>();
        loadFullList();
    }
    return quickcards;
}

public void loadFullList(){
    quickcards.postValue(repository.getAllCards());
}

// Repository gets data from Dao, which then gets data from Room database.   
...