Last active
August 29, 2015 14:20
-
-
Save exallium/1699437c06c5e8e05109 to your computer and use it in GitHub Desktop.
SugarDB Cursor Adapter for AutoComplete Widgets
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
| package org.exallium.tradetracker.app.controller.adapters | |
| import android.view.LayoutInflater | |
| import android.view.View | |
| import android.view.ViewGroup | |
| import android.widget.BaseAdapter | |
| import android.widget.Filter | |
| import android.widget.Filterable | |
| import android.widget.TextView | |
| import com.orm.query.Condition | |
| import com.orm.query.Select | |
| import org.exallium.tradetracker.app.R | |
| import org.exallium.tradetracker.app.model.entities.Record | |
| import java.util.ArrayList | |
| public open class AutoCompleteCursorAdapter<E : Record<E>>(val clazz: Class<E>, val filterField: String) : BaseAdapter(), Filterable { | |
| private var currentItems = ArrayList<E>() | |
| private fun defaultSelect(): Select<E> { | |
| return Select.from(clazz); | |
| } | |
| override fun getCount(): Int { | |
| return currentItems.size() | |
| } | |
| override fun getView(position: Int, convertView: View?, parent: ViewGroup): View? { | |
| var view : TextView? = convertView as TextView? | |
| if (view == null) { | |
| view = LayoutInflater.from(parent.getContext()).inflate(R.layout.support_simple_spinner_dropdown_item, parent, false) as TextView? | |
| } | |
| val obj = getItem(position) | |
| view?.setText(obj) | |
| return view | |
| } | |
| protected fun getRecord(position: Int): E { | |
| return currentItems.get(position) | |
| } | |
| override fun getItem(position: Int): String? { | |
| return "%s[%d]".format(clazz.getName(), getRecord(position)) | |
| } | |
| override fun getItemId(position: Int): Long { | |
| return currentItems.get(position).getId() | |
| } | |
| override fun getFilter(): Filter? { | |
| return RecordFilter() | |
| } | |
| private inner class RecordFilter : Filter() { | |
| override fun publishResults(constraint: CharSequence?, results: Filter.FilterResults) { | |
| currentItems.clear() | |
| currentItems.addAll(results.values as List<E>) | |
| notifyDataSetChanged() | |
| } | |
| override fun performFiltering(constraint: CharSequence?): Filter.FilterResults { | |
| val results = Filter.FilterResults() | |
| val select = defaultSelect().where(Condition.prop(filterField).like("%s%%".format(constraint))) | |
| results.count = select.count().toInt() | |
| results.values = select.list() | |
| return results | |
| } | |
| } | |
| } | |
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
| package org.exallium.tradetracker.app.model.entities | |
| public class Person : Record<Person>() { | |
| public var name: String = "" | |
| } |
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
| package org.exallium.tradetracker.app.controller.adapters | |
| import org.exallium.tradetracker.app.model.entities.Person | |
| public class PersonCursorAdapter : AutoCompleteCursorAdapter<Person>(javaClass<Person>(), "name") { | |
| override public fun getItem(position: Int): String? { | |
| return getRecord(position).name | |
| } | |
| } |
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
| package org.exallium.tradetracker.app.model.entities | |
| import com.orm.SugarRecord | |
| import org.exallium.tradetracker.app.controller.MainApplication | |
| public open class Record<T> : SugarRecord<T>() { | |
| override fun save() { | |
| super.save() | |
| // We should move to an Observer Model, where when we save a new Model, we emit it via an observer, and | |
| // it gets sent down the chain. The Adapter sees this new item, finds the appropriate position in the view | |
| // to add it, and notifyAdded at the appropriate location, or notifyModified if this is just a modification | |
| // (The item already exists in the list) | |
| MainApplication.onObjectSavedSubject.onNext(this) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment