Created
May 3, 2018 01:29
-
-
Save Felipe00/1127f186aff91effa6423e316ac02adf to your computer and use it in GitHub Desktop.
Exemplo de query com edittext e lista
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
| 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() | |
| } | |
| } |
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
| 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