Adding new element(s) to Spinner Adapter


Adding new element(s) to Spinner Adapter



I'm new to Kotlin language and I'm going to load Spinner data from the website. For this reason, I used Fuel Library as my httpGet,httpPost and ... helper library and simply the Spinner Control for showing that data to the user. Below is my tried code:


Kotlin


Spinner


httpGet


httpPost


Spinner


var listOfLesson:List<String> = listOf()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.ostad_page)

val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, listOfLesson)
"home/GetAllLessons".httpGet().responseString { request, response, result ->
when (result) {
is Result.Success -> {
val lessonsArray: List<tblLesson> =
Gson().fromJson(result.value, Array<tblLesson>::class.java).toList()
var index:Int = 0
for (lesson: tblLesson in lessonsArray) {
listOfLesson.plusElement(lesson.Title)
}
showSuccess("لیست دروس بارگذاری شد")
adapter.notifyDataSetChanged()
}
}
}
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
lessons.setAdapter(adapter)
adapter.notifyDataSetChanged()
}



As I traced Result.Success part, everything was ok and data was loaded successfully from URL into listOfLesson and adapter.notifyDataSetChanged() executed without any error but the result was nothing and no entry added to Spinner.
Thanks in advance :)


Result.Success


listOfLesson


adapter.notifyDataSetChanged()


Spinner




1 Answer
1



Calling plusElement on a List does not add an element to the existing list (remember, List in Kotlin is immutable). Rather, it creates a new list with the new item added. The original list (the one the adapter uses) remains unchanged. Furthermore, rather than adding to the list, what you should do is add the new items to the adapter directly. That way the adapter will update itself without you having to call notifyDataSetChanged().


plusElement


List


List


notifyDataSetChanged()


val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, ArrayList<String>())
"home/GetAllLessons".httpGet().responseString { request, response, result ->
when (result) {
is Result.Success -> {
Gson().fromJson(result.value, Array<tblLesson>::class.java)
.map { it.Title }
.let { adapter.addAll(it) }
showSuccess("لیست دروس بارگذاری شد")
}
}
}
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
lessons.setAdapter(adapter)





Thank you for answering but following error occurred: java.lang.UnsupportedOperationException: Operation is not supported for read-only collection
– Yashar Aliabasi
Jul 1 at 19:12


java.lang.UnsupportedOperationException: Operation is not supported for read-only collection





@YasharAliabasi I've changed the first line. You can remove listOfLesson, you don't need it anymore.
– Leo Aso
Jul 1 at 19:57


listOfLesson






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to make file upload 'Required' in Contact Form 7?

Rothschild family

amazon EC2 - How to make wp-config.php to writable?