android - 在自定义适配器 ListView 中突出显示搜索到的文本

标签 android listview android-listview listadapter

我有一个 ListView ,想从中搜索文本。我已经成功完成了,但现在我想搜索该项目并在 ListView 中突出显示搜索到的文本。这是我在 ListViewAdapter 中的过滤函数:

public void filter(String charText) {

    charText = charText.toLowerCase(Locale.getDefault());
    worldpopulationlist.clear();
    if (charText.length() == 0) {
        worldpopulationlist.addAll(arraylist);
    } 
    else 
    {
        for (WorldPopulation wp : arraylist) 
        {
            // Find charText in wp
            int startPos = wp.getCountry().toLowerCase(
                    Locale.getDefault()).indexOf(charText.toLowerCase(Locale.getDefault()));
            int endPos = startPos + charText.length();
            if (startPos != -1) 
            {
                   Spannable spannable = new SpannableString(wp.getCountry());
                    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);
                   //    countryTextView.setText(spannable);
                    worldpopulationlist.add(wp);

            }

        }
    }
    notifyDataSetChanged();

}

我用谷歌搜索了一下,我知道 Spannable 用于此目的,但它不起作用。如果您需要任何其他相关代码,请帮助我并告诉我。

编辑:

我遵循的教程来自here .我使用了相同的代码并做了一些小改动。我只想在 ListView 中突出显示搜索到的文本(在本例中只是一项,例如国家/地区)。

最佳答案

好吧,我下载了示例项目,最后得到了以下内容。根据您的需要调整代码。

在您的过滤方法中,存储用于执行过滤的字符串:

// Filter Class
public void filter(String searchString) {
    this.searchString = searchString;
    ...
    // Filtering stuff as normal.
}

您必须声明一个成员字符串来存储它:

public class ListViewAdapter extends BaseAdapter {
    ...    
    String searchString = "";
    ...

然后,在 getView 中突出显示搜索词:

public View getView(final int position, View view, ViewGroup parent) {
    ...
    // Set the results into TextViews
    WorldPopulation item = worldpopulationlist.get(position);
    holder.rank.setText(item.getRank());
    holder.country.setText(item.getCountry());
    holder.population.setText(item.getPopulation());

    // Find charText in wp
    String country = item.getCountry().toLowerCase(Locale.getDefault());
    if (country.contains(searchString)) {
        Log.e("test", country + " contains: " + searchString);
        int startPos = country.indexOf(searchString);
        int endPos = startPos + searchString.length();

        Spannable spanText = Spannable.Factory.getInstance().newSpannable(holder.country.getText()); // <- EDITED: Use the original string, as `country` has been converted to lowercase.
        spanText.setSpan(new ForegroundColorSpan(Color.RED), startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        holder.country.setText(spanText, TextView.BufferType.SPANNABLE);
    }
    ...
}

希望对您有所帮助。

关于android - 在自定义适配器 ListView 中突出显示搜索到的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29008824/

相关文章:

java - 如何在 firebase 数据库中获取嵌套键

android - 读取 ListView 的 EditText 的完成输入

android - 使用 LoaderManager 刷新 ListFragment

java - 在 Activity 之间来回传递数据

android - 在 Android 中搜索 ListView 项目,总是打开 ListView 的第一个项目

Android 开发人员 MyFirstApp - "menu cannot be resolved or is not a field"

android - 在 Android 中检测重叠路径

Android findViewById() with onFling in ListView 问题

android - 如何从 fragment 中调用 fragment

java - 自定义 ListView 中的弹出菜单