Skip to content

Instantly share code, notes, and snippets.

@Felipe00
Created May 3, 2018 01:29
Show Gist options
  • Select an option

  • Save Felipe00/1127f186aff91effa6423e316ac02adf to your computer and use it in GitHub Desktop.

Select an option

Save Felipe00/1127f186aff91effa6423e316ac02adf to your computer and use it in GitHub Desktop.
Exemplo de query com edittext e lista
class CityCodeAdapter: BaseAdapter {
val list = MutableList()
// Outros métodos obrigatórios do adapter
fun updateList(cityList: MutableList<CityCode>) {
this.list.clear()
this.list.addAll(cityList)
notifyDataSetChanged()
}
}
class SearchCityActivity : BaseActivity() {
private lateinit var cityAdapter: CityCodeAdapter
private val cityList: MutableList<CityCode> = ArrayList()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_search_city)
// instancia o adapter
cityAdapter = CityCodeAdapter(/* inicio o adapter */)
// passa o adapter pra listview ou recyclerview
// instancia o listener do edittext
searchText.setOnEditorActionListener({ textview, actionId, event ->
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
searchCities(textview.text.toString())
true
} else {
false
}
})
}
private fun searchCities(query: String?): Boolean {
if (!query.isNullOrBlank()) {
// faz uma consulta no banco e pega a nova lista
val listaNova = DB.queryListCity()
// seta a lista nova no adapter
cityAdapter.updateList(listaNova.toMutableList()) // toMutableList é um método do kotlin, não precisa usar ele.
}
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment