SpannableString regex in a ListView

做~自己de王妃 提交于 2019-12-30 07:39:09

问题


I have a ListView that I'm binding a collection of strings to, using a custom adapter. I'm also underlining certain keywords in the text. I'm using a SpannableString and a regular expression to underline the words, but I'm wondering if this is the most efficient way to do it? I'm noticing a lot of allocations in the Allocation Tracker of the java.util.regex.Matcher and the regex.util.regex.Pattern classes, which may be causing memory leaks in my app. I know regex's can be expensive, but I'm not sure another way to do what I need to do.

    public class Main extends ListActivity
    {
         private static CustomAdapter adapter = null;
private static List<Keyword> keywords;
private static Matcher matcher;

    @Override
    public void onCreate(Bundle icicle) 
    {  
            List<Item> items = new ArrayList<Item>();
    keywords = GetKeywords();
        items = GetItems();
            adapter = new CustomAdapter();

            for (Item item : items)
                adapter.addItem(item);

            this.setListAdapter(adapter);

    adapter.notifyDataSetChanged();
    }

      /* ADAPTER */
 private class CustomAdapter extends BaseAdapter 
      {      
    private final List<Item> mData = new ArrayList<Item>();
    private final LayoutInflater mInflater;
    public CustomAdapter() {
        mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void addItem(Item item) {
        mData.add(item);
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        final ViewHolder holder;
        final Item item = (Item)this.getItem(position);

        if (convertView == null)
        {
            holder = new ViewHolder();

            convertView = mInflater.inflate(R.layout.main, parent, false);

            holder.text = (TextView)convertView.findViewById(R.id.text);

            convertView.setTag(holder);
        } 
        else 
        {
            holder = (ViewHolder)convertView.getTag();
        }

            holder.text.setText(Highlight(item.getTitle(), keywords, matcher), BufferType.SPANNABLE);

        return(convertView);
    }
}

static class ViewHolder {
    TextView text, date, site;
}

private SpannableString Highlight(String text, List<Keyword> keywords, Matcher matcher)
{
    final SpannableString content = new SpannableString(text);

    for (Keyword keyword : keywords)
    {
        matcher = Pattern.compile("\\b" + keyword + "\\b").matcher(text);

        if (matcher.find())
        {
            start = matcher.start();
            end = matcher.end();
            content.setSpan(new UnderlineSpan(), start, end, 0);
        }
    }
    }


    return content;
    }
}

回答1:


You are creating a lot of Patterns and Matchers you don't need. I suggest you create one regex to match all the keywords, like this:

private SpannableString Highlight(String text, List<Keyword> keywords)
{
  final SpannableString content = new SpannableString(text);

  if (keywords.size() > 0)
  {
    /* create a regex of the form: \b(?:word1|word2|word3)\b */
    StringBuilder sb = ne StringBuilder("\\b(?:").append(keywords.get(0).toString());
    for (int i = 1; i < keywords.size(); i++)
    {
      sb.append("|").append(keywords.get(i).toString());
    }
    sb.append(")\\b");

    Matcher m = Pattern.compile(sb.toString()).matcher(text);

    while (m.find())
    {
      content.setSpan(new UnderlineSpan(), m.start(), m.end(), 0);
    }
  }

  return content;
}

Pattern objects are quite expensive to create, so that's where your real savings will come from. On the other hand, Matchers are relatively cheap, which is why I switched from using a static instance to creating a new one each time.



来源:https://stackoverflow.com/questions/7830128/spannablestring-regex-in-a-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!