33import android .content .Context ;
44import android .database .Cursor ;
55import android .support .v4 .widget .CursorAdapter ;
6+ import android .text .TextUtils ;
67import android .view .LayoutInflater ;
78import android .view .View ;
89import android .view .ViewGroup ;
@@ -20,12 +21,35 @@ public ReaderSearchSuggestionAdapter(Context context) {
2021 super (context , null , false );
2122 }
2223
23- public void populate (String filter ) {
24+ /*
25+ * populate the adapter using a cursor containing past searches that match the filter
26+ */
27+ public void setFilter (String filter ) {
28+ // skip if unchanged
29+ if (isCurrentFilter (filter ) && getCursor () != null ) {
30+ return ;
31+ }
2432 Cursor cursor = ReaderSearchTable .getQueryStringCursor (filter , MAX_SUGGESTIONS );
2533 swapCursor (cursor );
2634 mCurrentFilter = filter ;
2735 }
2836
37+ /*
38+ * forces setFilter() to always repopulate by skipping the isCurrentFilter() check
39+ */
40+ private void reload () {
41+ String newFilter = mCurrentFilter ;
42+ mCurrentFilter = null ;
43+ setFilter (newFilter );
44+ }
45+
46+ private boolean isCurrentFilter (String filter ) {
47+ if (TextUtils .isEmpty (filter ) && TextUtils .isEmpty (mCurrentFilter )) {
48+ return true ;
49+ }
50+ return filter .equalsIgnoreCase (mCurrentFilter );
51+ }
52+
2953 public String getSuggestion (int position ) {
3054 Cursor cursor = (Cursor ) getItem (position );
3155 if (cursor != null ) {
@@ -38,6 +62,7 @@ public String getSuggestion(int position) {
3862 private class SuggestionViewHolder {
3963 private final TextView txtSuggestion ;
4064 private final ImageView imgDelete ;
65+
4166 SuggestionViewHolder (View view ) {
4267 txtSuggestion = (TextView ) view .findViewById (R .id .text_suggestion );
4368 imgDelete = (ImageView ) view .findViewById (R .id .image_delete );
@@ -61,7 +86,7 @@ public void bindView(View view, Context context, Cursor cursor) {
6186 @ Override
6287 public void onClick (View v ) {
6388 ReaderSearchTable .deleteQueryString (query );
64- populate ( mCurrentFilter );
89+ reload ( );
6590 }
6691 });
6792 }
0 commit comments