Program: 33875
Project: 33875
Buggy source:
private void updateQuery(final String query){
  String finalQuery=Utilities.getPreparedQueryForSearch(query);
  ArrayList<ListItem> listItems=new ArrayList<>();
  Cursor teamQueryResults=Database.getInstance(this).getMatchesForTeamQuery(finalQuery);
  if (teamQueryResults != null && teamQueryResults.moveToFirst()) {
    teamQueryResults.moveToPosition(-1);
    teamsHeader=new SearchResultsHeaderListElement(getString(R.string.teams_header));
    if (teamQueryResults.getCount() >= MAX_RESULTS_PER_CATEGORY) {
      teamsHeader.showMoreButton(true);
      teamsHeader.setMoreCount(teamQueryResults.getCount() - MAX_RESULTS_PER_CATEGORY);
    }
 else {
      teamsHeader.showMoreButton(false);
    }
    listItems.add(teamsHeader);
    while (teamQueryResults.moveToNext()) {
      if (teamQueryResults.getPosition() >= MAX_RESULTS_PER_CATEGORY) {
        break;
      }
      String key=teamQueryResults.getString(teamQueryResults.getColumnIndex(Database.SearchTeam.KEY));
      Team team=Database.getInstance(this).getTeam(key);
      TeamListElement element=new TeamListElement(team);
      listItems.add(element);
      Log.d(Constants.LOG_TAG,"String_Node_Str" + teamQueryResults.getString(teamQueryResults.getColumnIndex(Database.SearchTeam.TITLES)));
    }
  }
 else {
    teamsHeader=new SearchResultsHeaderListElement(getString(R.string.teams_header));
    teamsHeader.showMoreButton(false);
    listItems.add(teamsHeader);
    listItems.add(new EmptyListElement(getString(R.string.no_teams_found)));
  }
  Cursor eventQueryResults=Database.getInstance(this).getMatchesForEventQuery(finalQuery);
  if (eventQueryResults != null && eventQueryResults.moveToFirst()) {
    eventQueryResults.moveToPosition(-1);
    eventsHeader=new SearchResultsHeaderListElement(getString(R.string.events_header));
    if (teamQueryResults.getCount() >= MAX_RESULTS_PER_CATEGORY) {
      eventsHeader.showMoreButton(true);
      eventsHeader.setMoreCount(teamQueryResults.getCount() - MAX_RESULTS_PER_CATEGORY);
    }
 else {
      eventsHeader.showMoreButton(false);
    }
    listItems.add(eventsHeader);
    while (eventQueryResults.moveToNext()) {
      if (eventQueryResults.getPosition() >= MAX_RESULTS_PER_CATEGORY) {
        break;
      }
      String key=eventQueryResults.getString(eventQueryResults.getColumnIndex(Database.SearchEvent.KEY));
      Event event=Database.getInstance(this).getEvent(key);
      EventListElement element=new EventListElement(event);
      listItems.add(element);
    }
  }
 else {
    eventsHeader=new SearchResultsHeaderListElement(getString(R.string.events_header));
    eventsHeader.showMoreButton(false);
    listItems.add(eventsHeader);
    listItems.add(new EmptyListElement(getString(R.string.no_events_found)));
  }
  ListViewAdapter adapter=new ListViewAdapter(this,listItems);
  resultsList.setAdapter(adapter);
  resultsList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
    @Override public void onItemClick(    AdapterView<?> adapterView,    View view,    int position,    long id){
      ListViewAdapter adapter=(ListViewAdapter)adapterView.getAdapter();
      ListItem clickedItem=adapter.getItem(position);
      if (clickedItem instanceof TeamListElement) {
        String teamKey=((ListElement)clickedItem).getKey();
        Intent i=new Intent(SearchResultsActivity.this,ViewTeamActivity.class);
        i.putExtra(ViewTeamActivity.TEAM_KEY,teamKey);
        startActivity(i);
      }
 else       if (clickedItem instanceof EventListElement) {
        String eventKey=((ListElement)clickedItem).getKey();
        Intent intent=ViewEventActivity.newInstance(SearchResultsActivity.this,eventKey);
        startActivity(intent);
      }
 else       if (clickedItem == teamsHeader) {
        if (teamsHeader.isShowingMoreButton()) {
          startActivity(MoreSearchResultsActivity.newInstance(SearchResultsActivity.this,MoreSearchResultsActivity.TEAM_RESULTS,query));
        }
      }
 else       if (clickedItem == eventsHeader) {
        if (eventsHeader.isShowingMoreButton()) {
          startActivity(MoreSearchResultsActivity.newInstance(SearchResultsActivity.this,MoreSearchResultsActivity.EVENT_RESULTS,query));
        }
      }
    }
  }
);
}

Fixed version (ground truth):
private void updateQuery(final String query){
  String finalQuery=Utilities.getPreparedQueryForSearch(query);
  ArrayList<ListItem> listItems=new ArrayList<>();
  Cursor teamQueryResults=Database.getInstance(this).getMatchesForTeamQuery(finalQuery);
  if (teamQueryResults != null && teamQueryResults.moveToFirst()) {
    teamQueryResults.moveToPosition(-1);
    teamsHeader=new SearchResultsHeaderListElement(getString(R.string.teams_header));
    if (teamQueryResults.getCount() >= MAX_RESULTS_PER_CATEGORY) {
      teamsHeader.showMoreButton(true);
      teamsHeader.setMoreCount(teamQueryResults.getCount() - MAX_RESULTS_PER_CATEGORY);
    }
 else {
      teamsHeader.showMoreButton(false);
    }
    listItems.add(teamsHeader);
    while (teamQueryResults.moveToNext()) {
      if (teamQueryResults.getPosition() >= MAX_RESULTS_PER_CATEGORY) {
        break;
      }
      String key=teamQueryResults.getString(teamQueryResults.getColumnIndex(Database.SearchTeam.KEY));
      Team team=Database.getInstance(this).getTeam(key);
      TeamListElement element=new TeamListElement(team);
      listItems.add(element);
      Log.d(Constants.LOG_TAG,"String_Node_Str" + teamQueryResults.getString(teamQueryResults.getColumnIndex(Database.SearchTeam.TITLES)));
    }
  }
 else {
    teamsHeader=new SearchResultsHeaderListElement(getString(R.string.teams_header));
    teamsHeader.showMoreButton(false);
    listItems.add(teamsHeader);
    listItems.add(new EmptyListElement(getString(R.string.no_teams_found)));
  }
  Cursor eventQueryResults=Database.getInstance(this).getMatchesForEventQuery(finalQuery);
  if (eventQueryResults != null && eventQueryResults.moveToFirst()) {
    eventQueryResults.moveToPosition(-1);
    eventsHeader=new SearchResultsHeaderListElement(getString(R.string.events_header));
    if (eventQueryResults.getCount() >= MAX_RESULTS_PER_CATEGORY) {
      eventsHeader.showMoreButton(true);
      eventsHeader.setMoreCount(teamQueryResults.getCount() - MAX_RESULTS_PER_CATEGORY);
    }
 else {
      eventsHeader.showMoreButton(false);
    }
    listItems.add(eventsHeader);
    while (eventQueryResults.moveToNext()) {
      if (eventQueryResults.getPosition() >= MAX_RESULTS_PER_CATEGORY) {
        break;
      }
      String key=eventQueryResults.getString(eventQueryResults.getColumnIndex(Database.SearchEvent.KEY));
      Event event=Database.getInstance(this).getEvent(key);
      EventListElement element=new EventListElement(event);
      listItems.add(element);
    }
  }
 else {
    eventsHeader=new SearchResultsHeaderListElement(getString(R.string.events_header));
    eventsHeader.showMoreButton(false);
    listItems.add(eventsHeader);
    listItems.add(new EmptyListElement(getString(R.string.no_events_found)));
  }
  ListViewAdapter adapter=new ListViewAdapter(this,listItems);
  resultsList.setAdapter(adapter);
  resultsList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
    @Override public void onItemClick(    AdapterView<?> adapterView,    View view,    int position,    long id){
      ListViewAdapter adapter=(ListViewAdapter)adapterView.getAdapter();
      ListItem clickedItem=adapter.getItem(position);
      if (clickedItem instanceof TeamListElement) {
        String teamKey=((ListElement)clickedItem).getKey();
        Intent i=new Intent(SearchResultsActivity.this,ViewTeamActivity.class);
        i.putExtra(ViewTeamActivity.TEAM_KEY,teamKey);
        startActivity(i);
      }
 else       if (clickedItem instanceof EventListElement) {
        String eventKey=((ListElement)clickedItem).getKey();
        Intent intent=ViewEventActivity.newInstance(SearchResultsActivity.this,eventKey);
        startActivity(intent);
      }
 else       if (clickedItem == teamsHeader) {
        if (teamsHeader.isShowingMoreButton()) {
          startActivity(MoreSearchResultsActivity.newInstance(SearchResultsActivity.this,MoreSearchResultsActivity.TEAM_RESULTS,query));
        }
      }
 else       if (clickedItem == eventsHeader) {
        if (eventsHeader.isShowingMoreButton()) {
          startActivity(MoreSearchResultsActivity.newInstance(SearchResultsActivity.this,MoreSearchResultsActivity.EVENT_RESULTS,query));
        }
      }
    }
  }
);
}