Created
November 20, 2015 11:28
-
-
Save sdex/83b75ce9c2f2e2654bec to your computer and use it in GitHub Desktop.
Android: AutoCompleteTextView with Realm.io database
This file contains hidden or 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
/* | |
* Copyright 2015 Yuriy Mysochenko | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
public class CountryAutoCompleteAdapter extends BaseAdapter implements Filterable { | |
private Context mContext; | |
private List<Country> mResult = new ArrayList<>(); | |
public CountryAutoCompleteAdapter(Context context) { | |
mContext = context; | |
} | |
@Override | |
public int getCount() { | |
return mResult.size(); | |
} | |
@Override | |
public Country getItem(int index) { | |
return mResult.get(index); | |
} | |
@Override | |
public long getItemId(int position) { | |
return position; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
if (convertView == null) { | |
LayoutInflater inflater = (LayoutInflater) mContext | |
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
convertView = inflater.inflate(android.R.layout.simple_spinner_dropdown_item, parent, false); | |
} | |
((TextView) convertView.findViewById(android.R.id.text1)).setText(getItem(position).getName()); | |
return convertView; | |
} | |
@Override | |
public Filter getFilter() { | |
return new Filter() { | |
@Override | |
protected FilterResults performFiltering(CharSequence constraint) { | |
return null; | |
} | |
@Override | |
protected void publishResults(CharSequence constraint, FilterResults results) { | |
// filter data in UI thread instead of background one because of Realm limitation: | |
// the data cannot be passed across threads | |
if (constraint != null) { | |
String query = constraint.toString().toLowerCase(); | |
mResult = filterCountries(query); | |
notifyDataSetChanged(); | |
} else { | |
notifyDataSetInvalidated(); | |
} | |
}}; | |
} | |
@NonNull | |
private List<Country> filterCountries(String query) { | |
Realm mRealm = Realm.getDefaultInstance(); | |
return mRealm | |
.where(Country.class) | |
.beginsWith("name", query, false) | |
.findAll(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, for anybody who finds this like I did: you should NEVER open a Realm instance and then not close it - Realm should probably be passed here from the enclosing activity which manages its lifecycle.