Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6739663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:30:56+00:00 2026-05-26T11:30:56+00:00

I have a ListView that I’m binding a collection of strings to, using a

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:30:56+00:00Added an answer on May 26, 2026 at 11:30 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ListView that uses a custom adapter. The custom adapter's getView uses
I have a ListView that is populated using an XML file. However, I want
I have a ListView that uses a customized adapter, but I can't click on
I have a listview that generates thumbnail using a backgroundworker. When the listview is
I have a ListView that I'm populating from a custom ListAdapter . Inside the
I have a ListView that uses a custom layout that has 5 child View
I have a ListView that contains a large collection of rows with textboxes that
I have a ListView that I need to update by adding an item. Using
I have this ListView that displays a custom user control. The custom user control
I have ListView that has the following EditItemTemplate: <EditItemTemplate> <tr style=> <td> <asp:LinkButton ID=UpdateButton

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.