Permalink
Browse files

Adding Search

  • Loading branch information...
dragosholban committed Mar 10, 2018
1 parent dd09b5f commit 0929413c3e6f06ee757175bbb3066de12d611e0f
@@ -1,12 +1,17 @@
package dragosholban.com.timezoneconverter;
import android.content.Intent;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Filter;
import android.widget.ListView;
import java.util.ArrayList;
@@ -15,7 +20,7 @@
public class SelectTimezonesActivity extends AppCompatActivity {
ArrayList<String> selectedTimezones = new ArrayList<>();
ArrayAdapter<String> adapter;
TimeZoneAdapter adapter;
ArrayList<String> timezones;
ListView listView;
boolean showAll = true;
@@ -33,7 +38,7 @@ protected void onCreate(Bundle savedInstanceState) {
timezones = new ArrayList<>(Arrays.asList(TimeZone.getAvailableIDs()));
listView = findViewById(R.id.listView);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_multiple_choice, android.R.id.text1, timezones);
adapter = new TimeZoneAdapter(this, android.R.layout.simple_list_item_multiple_choice, android.R.id.text1, timezones);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@@ -97,4 +102,33 @@ private void checkSelectedTimezones() {
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
adapter.getFilter().filter(s, new Filter.FilterListener() {
@Override
public void onFilterComplete(int i) {
checkSelectedTimezones();
}
});
return true;
}
});
return true;
}
}
@@ -1,8 +1,12 @@
package dragosholban.com.timezoneconverter;
import android.content.Intent;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
@@ -13,6 +17,7 @@
import java.util.TimeZone;
public class TimeZoneActivity extends AppCompatActivity {
TimeZoneAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -21,7 +26,7 @@ protected void onCreate(Bundle savedInstanceState) {
ListView listView = findViewById(R.id.listView);
final ArrayList<String> timezones = new ArrayList<>(Arrays.asList(TimeZone.getAvailableIDs()));
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, timezones);
adapter = new TimeZoneAdapter(this, android.R.layout.simple_list_item_1, android.R.id.text1, timezones);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@@ -34,4 +39,28 @@ public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
adapter.getFilter().filter(s);
return true;
}
});
return true;
}
}
@@ -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();
}
}
}
@@ -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>

0 comments on commit 0929413

Please sign in to comment.