1

カスタムアダプタを使用して、文字列のコレクションをバインドしているListViewがあります。また、テキスト内の特定のキーワードに下線を引いています。SpannableStringと正規表現を使用して単語に下線を付けていますが、これが最も効率的な方法であるかどうか疑問に思っています。java.util.regex.Matcherクラスとregex.util.regex.PatternクラスのAllocationTrackerに多くの割り当てがあり、アプリでメモリリークが発生している可能性があります。正規表現は高額になる可能性があることは知っていますが、必要なことを行う別の方法がわかりません。

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

1 に答える 1

3

必要のない多くのパターンとマッチャーを作成しています次のように、すべてのキーワードに一致する 1 つの正規表現を作成することをお勧めします。

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

パターン オブジェクトの作成にはかなりのコストがかかるため、実際の節約はそこから得られます。一方、Matcher は比較的安価であるため、静的インスタンスの使用から毎回新しいインスタンスの作成に切り替えました。

于 2011-10-20T14:38:26.837 に答える