1

The Adapter code below for the RecyclerView successfully highlights user entered text (using an EditText line linked to a SearchView) to the color Green.

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);
        }  

I tried the following code addition using removeSpan() in the Answer here: Android Spannable: how to clear color? to clear the color when the query does not return any items, but with no luck. What am I missing here?

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.   
AJW
  • 1,578
  • 3
  • 36
  • 77
  • Call `removeSpan()` to remove a span from a `Spannable`. See [this answer](https://stackoverflow.com/a/18157455/115145) for an example. – CommonsWare Oct 23 '19 at 23:46
  • @CommonsWare I tried the following code addition using removeSpan() in the Answer here: https://stackoverflow.com/questions/45900950/android-spannable-how-to-clear-color to clear the color when the query does not return any items, but with no luck. – AJW Oct 23 '19 at 23:48
  • 1
    You might consider then editing your question and showing how you are trying to use `removeSpan()`. Neither of your code snippets shows `removeSpan()`. Removing a span from a `Spannable` has little to do with `RecyclerView` and even less to do with `ViewModel`. – CommonsWare Oct 23 '19 at 23:54
  • will do, updating now. – AJW Oct 23 '19 at 23:55
  • 1
    @Commonsware Ok, so your advice made me re-think my logic in the Activity that calls the Adapter update. Turns out that was the culprit. The removeSpan() works fine to return the text to the default color. – AJW Oct 24 '19 at 00:15

0 Answers0