【问题标题】:How to change textview background color in listview-cursoradapter when input text matches with cursor string?当输入文本与光标字符串匹配时,如何更改 listview-cursoraapter 中的 textview 背景颜色?
【发布时间】:2015-08-16 04:06:25
【问题描述】:

我正在尝试在列表视图中实现搜索功能,例如浏览器中的文本搜索。

我已成功在光标数据中搜索输入词。但是找到匹配时如何更改相应单词的背景颜色?

这是bindview中的搜索代码

if (searchEnabled) {

    cursor.moveToFirst();

    while (cursor.moveToNext()) {

        String cursorText = cursor.getString(indexMessage);
        if (s.equals(searchText.toString())) {

            //Change matched word's(textview) background color here
            holder.textMessage.setBackgroundColor(Color.WHITE);
            Log.i("Cursor Scan:", cursorText);
        }
    }
}

调用notifyDataSetChanged()后,只改变最后一行文本视图的颜色。

我尝试通过从getChildAt() 获取列表视图文本进行搜索并部分成功,但此视图不一致,它在滚动时达到其原始状态(因为此列表视图从光标膨胀)。

我尝试使用以下代码在列表视图文本中进行搜索:

private void updateListView(String textForSearch) {

    LocalStoreDB localDB = new LocalStoreDB(getApplicationContext());
    localMessageDB.openDB();
    Cursor cursorForSearch = localDB.selectMessageDB();
    cursorForSearch.moveToFirst();

    while (cursorForSearch.moveToNext()) {

        int position = cursorForSearch.getPosition();

        View v = listview.getChildAt(position);

        TextView tv = (TextView) v.findViewById(R.id.textMessage);

        SpannableString cursorString = new SpannableString(tv.getText());
        Pattern p = Pattern.compile(textForSearch);
        Matcher m = p.matcher(cursorString);
        while (m.find()) {

            cursorString.setSpan(new BackgroundColorSpan(Color.WHITE),
                    m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            Log.i("m.find()", cursorString.toString());
        }
        tv.setText(cursorString);
    }

    localDB.closeDB();
}

【问题讨论】:

    标签: android listview


    【解决方案1】:

    抱歉各位迟到了。

    我已经在另一个帖子中回答了这个问题。请点击此链接: https://stackoverflow.com/a/34287029/4688535

    【讨论】:

      【解决方案2】:

      你可以像这样设置文本视图的背景颜色

      yourTextView.setBackgroundColor("0xff0000ff");
      

      我看不到其余代码,但也许您必须通知您在设置颜色后所做的更改。

      希望对你有帮助

      在你的情况下,我会尝试这样的事情:

      TextView tv;    
      for(int i=0;i<listview.get.getCount();i++){
      
         tv =(TextView) listview.getChildAt(i).findViewById(R.id.textMessage);
         if(tv.getText().equalsIgnoreCase(searchstring)){
      
             tv.setBackgroundColor("0xff0000ff");
         } 
         else{
             tv.setBackgroundColor("0xffffffff");
         }
      }
      
       listViewAdapter.notifyDataSetChanged();
      

      【讨论】:

      • 是的。我试过了,但它只改变最后一行 textview 的背景颜色。
      • 如何通知变更?
      • 你可以这样通知:yourArrayAdapterForListView.notifyDataSetChanged();但我想其他的东西会导致问题。你如何填写你的列表视图?您在使用自定义数据适配器吗?
      • 我正在使用自定义cursoradapter。我试过notifyDataSetChanged(),但问题是,它不会扫描所有行,直到数据集发生变化(在这种情况下是光标)。
      • 您的列表视图项目是否有唯一标识符,我的意思是您可以在不使用光标遍历的情况下识别另一个项目吗?如果是这样,请尝试在您的 while 循环中使用此标识符,例如“如果您搜索的字符串等于列表元素文本值,则获取列表视图项标识符”,然后设置颜色。我的意思不仅仅是获取光标引用的元素。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      • 2010-10-15
      • 1970-01-01
      • 2015-07-07
      相关资源
      最近更新 更多