У меня есть проект, в котором я получаю данные через залп для отображения в виде переработчика, но мои данные получают через класс модели представления. Как заполнить список, в котором будут храниться мои данные, для передачи в программу повторного просмотра. Я уже передал данные в просмотр повторной проверки через модель представления, но у меня возникли проблемы с тем, какой тип коллекции / списка / массива использовать для передачи данных.
Вот что я сделал.
private var classDetailList: List<ClassDetailsItem>? = null
private var timeTableList: List<TimeTableItem>? = null
val myBackgroundList : List<Int> = listOf(
R.drawable.light_green_gradient_curve,
R.drawable.light_blue_gradient_curve,
R.drawable.fiery_rose_gradient_curve,
R.drawable.blue_cerulean_gradient_curve
)
val modayClasseList: List<ClassDetailsItem> = listOf(
ClassDetailsItem("09:00 - 10:15", "Biology",
"Anatomy 101", "Class 1A", R.drawable.light_green_gradient_curve),
ClassDetailsItem("12:15 - 13:00", "Biology",
"Reproductive System", "Class 3C", R.drawable.light_blue_gradient_curve))
val tuesdayClasseList: List<ClassDetailsItem> = listOf(
ClassDetailsItem("09:00 - 10:15", "Agriculture",
"Plant Life", "Class 1B", R.drawable.fiery_rose_gradient_curve),
ClassDetailsItem("11:00 - 12:15", "Biology",
"Anatomy 102", "Class 3C", R.drawable.blue_cerulean_gradient_curve),
ClassDetailsItem("13:15 - 14:00", "Biology",
"Human Brain", "Class 3B", R.drawable.light_blue_gradient_curve))
val wednesdayClasseList: List<ClassDetailsItem> = listOf(
ClassDetailsItem("11:00 - 12:15", "Agriculture",
"Animal Husbandry", "Class 1A", R.drawable.fiery_rose_gradient_curve),
ClassDetailsItem("1300 - 14:15", "Biology",
"The Eyes", "Class 2B", R.drawable.light_green_gradient_curve))
val thursdayClasseList: List<ClassDetailsItem> = listOf(
ClassDetailsItem("09:00 - 10:15", "Agriculture",
"Plant Cells", "Class 3C", R.drawable.blue_cerulean_gradient_curve),
ClassDetailsItem("13:15 - 14:00", "Biology",
"Atom & Nucleus", "Class 2B", R.drawable.light_green_gradient_curve))
val fridayClasseList: List<ClassDetailsItem> = listOf(
ClassDetailsItem("09:00 - 10:15", "Agriculture",
"Soil", "Class 2C", R.drawable.light_blue_gradient_curve),
ClassDetailsItem("11:00 - 12:15", "Biology",
"Muscles", "Class 1B", R.drawable.fiery_rose_gradient_curve),
ClassDetailsItem("13:15 - 14:00", "Biology",
"Human Lungs", "Class 3A", R.drawable.light_green_gradient_curve))
private val originalList: List<TimeTableItem> = listOf(
TimeTableItem("Monday", "01 October 2018", "1st Term", modayClasseList),
TimeTableItem( "Tuesday", "02 October 2018", "1st Term", tuesdayClasseList),
TimeTableItem( "Wednesday", "03 October 2018", "1st Term", wednesdayClasseList),
TimeTableItem("Thursday", "04 October 2018", "1st Term", thursdayClasseList),
TimeTableItem( "Friday", "05 October 2018", "1st Term", fridayClasseList),
TimeTableItem( "Monday", "14 January 2019", "2nd Term",modayClasseList),
TimeTableItem( "Tuesday", "15 January 2019", "2nd Term", tuesdayClasseList),
TimeTableItem( "Wednesday", "16 January 2019", "2nd Term", wednesdayClasseList),
TimeTableItem( "Thursday", "17 January 2019", "2nd Term", thursdayClasseList),
TimeTableItem( "Friday", "18 January 2019", "2nd Term", fridayClasseList),
TimeTableItem( "Monday", "13 May 2019", "3rd Term", modayClasseList),
TimeTableItem("Tuesday", "14 May 2019", "3rd Term", tuesdayClasseList),
TimeTableItem("Wednesday", "15 May 2019", "3rd Term", wednesdayClasseList),
TimeTableItem( "Thursday", "16 May 2019", "3rd Term", thursdayClasseList),
TimeTableItem( "Friday", "17 May 2019", "3rd Term", fridayClasseList)
)
private fun loadTimeTableItems() {
val service = ServiceVolley()
val apiController = ApiController(service)
val path = "getUserClasses.php"
val params = JSONObject()
params.put("teacherid", "francola")
apiController.post(path, params) {response ->
val obj = JSONObject(response.toString())
if(obj.getInt("status") == 1) {
val arrayAll = obj.getJSONArray("result")
for(i in 0 until arrayAll.length()) {
val allData = arrayAll.getJSONObject(i)
val classData = allData.getJSONObject("class")
classDetailList = listOf(ClassDetailsItem(classData.getString("from") + " - " + classData.getString("to"),
classData.getString("name"), classData.getString("topicId"), classData.getString("class"),
myBackgroundList.shuffled().take(1)[0]))
val timeTableItem = TimeTableItem(allData.getString("DayofWeek"), allData.getString("DayofWeek"),
allData.getString("term"), classDetailList!!)
// timeTableList = list
// = listOf(timeTableItem)
}
}
}
}
val filteredPosts: MutableList<TimeTableItem> = mutableListOf()
val oldFilteredPosts: MutableList<TimeTableItem> = mutableListOf()
init {
loadTimeTableItems()
oldFilteredPosts.addAll(originalList)
}
fun searchSubjects(query: String): Completable = Completable.create {
val wanted = originalList.filter {
it.subjectClasses.isNotEmpty()
}.map { timeTableItem -> TimeTableItem(timeTableItem.dayOfWeek, timeTableItem.calDate, timeTableItem.schoolTerm,
timeTableItem.subjectClasses.filter { it.subject == query }.requireNoNulls().toList())}.toList()
filteredPosts.clear()
filteredPosts.addAll(wanted)
it.onComplete()
}
fun searchTerms(query: String): Completable = Completable.create {
val wanted = originalList.filter {
it.schoolTerm == query
}.toList()
filteredPosts.clear()
filteredPosts.addAll(wanted)
it.onComplete()
}
fun searchTermsSubject(query: String, subjQuery: String): Completable = Completable.create {
val wanted = originalList.filter {
it.schoolTerm == query
}.map { timeTableItem -> TimeTableItem(timeTableItem.dayOfWeek, timeTableItem.calDate, timeTableItem.schoolTerm,
timeTableItem.subjectClasses.filter { it.subject == subjQuery }.toList()) }.toList()
filteredPosts.clear()
filteredPosts.addAll(wanted)
it.onComplete()
}