Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Adding Search
- Loading branch information
1 parent
dd09b5f
commit 0929413
Showing
4 changed files
with
136 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
app/src/main/java/dragosholban/com/timezoneconverter/TimeZoneAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package dragosholban.com.timezoneconverter; | ||
|
||
import android.content.Context; | ||
import android.support.annotation.NonNull; | ||
import android.widget.ArrayAdapter; | ||
import android.widget.Filter; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class TimeZoneAdapter extends ArrayAdapter<String> { | ||
|
||
private ArrayList<String> original; | ||
private Filter filter; | ||
|
||
public TimeZoneAdapter(@NonNull Context context, int resource, int textViewResourceId, @NonNull ArrayList<String> objects) { | ||
super(context, resource, textViewResourceId, objects); | ||
original = new ArrayList<>(objects); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Filter getFilter() { | ||
return new TimeZoneFilter(); | ||
} | ||
|
||
private class TimeZoneFilter extends Filter { | ||
|
||
@Override | ||
protected FilterResults performFiltering(CharSequence charSequence) { | ||
FilterResults results = new FilterResults(); | ||
ArrayList<String> filtered = new ArrayList<>(); | ||
String search = charSequence.toString().toLowerCase(); | ||
|
||
if (search == null || search.length() == 0) { | ||
filtered = new ArrayList<>(original); | ||
} else { | ||
for (int i = 0; i < original.size(); i++) { | ||
if (original.get(i).toLowerCase().contains(charSequence)) { | ||
filtered.add(original.get(i)); | ||
} | ||
} | ||
} | ||
|
||
results.values = filtered; | ||
results.count = filtered.size(); | ||
|
||
return results; | ||
} | ||
|
||
@Override | ||
protected void publishResults(CharSequence charSequence, FilterResults filterResults) { | ||
ArrayList<String> items = (ArrayList<String>) filterResults.values; | ||
clear(); | ||
for (int i = 0; i < items.size(); i++) { | ||
add(items.get(i)); | ||
} | ||
|
||
notifyDataSetChanged(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<menu xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto"> | ||
<item android:id="@+id/action_search" | ||
android:icon="@android:drawable/ic_menu_search" | ||
app:showAsAction="ifRoom|collapseActionView" | ||
app:actionViewClass="android.support.v7.widget.SearchView" | ||
android:title="@string/app_name" /> | ||
</menu> |