'How to send multiple items of recyclerview from one activity to another
I have an ArrayList of type Course. Course has id ( string ), name ( string ).
I want to display a list of courses on one screen. Give the user option to select multiple courses which they have completed and send these courses to next activity.
I am able to MultiSelect courses in a RecyclerView. But unable to send the data to another activity.
Solution 1:[1]
you can convert the data to JSON string & pass easily between activities using intent bundles...
create extension functions like these
// convert safely from json to class
fun <T> String.fromSafeJson(classOfT: Class<T>): T? {
return try {
Gson().fromJson(this, classOfT)
} catch (e: Exception) {
null
}
}
// convert any object to json
fun Any.toJson(): String {
return Gson().toJson(this)
}
after that you can convert any list of any type using yourList.toJson(), send it via bundle and in next activity get it from bundle and parse using stringName.fromSafeJson(YourListType)
remember to add Gson library... use can use the following
implementation 'com.google.code.gson:gson:2.9.0'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Saksham Khurana |
