I have multiple select option and I can receive their ids what I need is to make array of those ids in order to send them to server.
Why I want to make array of my array?
Because my main array includes other information and I just need to extract (make array) of ids not any other values such as names.
Example
I will select 2 items and I receive
id->1
name->one
id->2
name->two
Then I just need to make array like:
items[
0->1
1->2
]
Code
private fun printSelectedValue() {
serviceAdapter?.getList()?.let {
it.forEach { service ->
Log.e("printSelectedValue", "selected service name:- ${service.service.name}")
// I need array of this (service.id)
Log.e("printSelectedValue", "selected service id:- ${service.id}")
}
}
}
PS: I need to be able to use this array (array of ids) in another function so it's preferred to have private value in my fragment in order to access it in other function
Any idea?
fun getSelectedIds(): Map<Int, Int> {
val items = serviceAdapter?.getList() ?: return
return items.withIndex().associate { (index, item) -> index to item.id }
}
I am just giving you an example, change this code according to your use case
In your data class add an extra field for detecting the item is selected or not
so your data class should look like this
data class Item(val id: Int, val name: String, var isSelected: Boolean)
While selecting deselecting item change the value of isSelected accordingly.
Then before sending the value populate an empty ArrayList from selected ArrayList like this
val selectedList = actualDataSet.filter { it.isSelected }
var ids = ArrayList<Int>()
selectedList.forEach { service ->
ids.add(service.id)
}
//here you will get the ArrayList of selected items' id
Related
I want to remove a object from the list and so that i can add just required string and pass it . i have a model class like this
data class TagItem(
val tagTittle: String,
val isSelected: Boolean
)
this data class is mapped for the Lazy Column for making the list select and deSelect the items
var tagsItems by remember {
mutableStateOf(
(tagsList).map {
TagItem(
tagTittle = it,
isSelected = false
)
}
)
}
val productEveryTags = tagsItems.filter {
it.isSelected
}
Log.i(TAG,"Only this $productEveryTags ")
viewModel.onEvent(ProductUploadEvent.EnteredProductTags(productEveryTags))
i am filtering the selected items alone but in my log statement
Only this [TagItem(tagTittle=Tagged , isSelected=true), TagItem(tagTittle=Ducati , isSelected=true)]
How can i remove the "isSelected" object and just get the "tagTittle" alone into a single List
You can simply map your instances for the output:
Log.i(TAG,"Only this ${productEveryTags.map { it.tagTittle }}")
Or combine it with the existing filter. Depending on whether you are interested in duplicates, you can also directly map to a set:
val productEveryTags = tagsItems.filter {
it.isSelected
}.mapTo(LinkedHashSet()) {
it.tagTittle
}
Log.i(TAG,"Only this $productEveryTags")
I am trying to extract from a list all element which are not in the sublist already created. I cannot make it work using .filterNot because it filtering on the overall data class store in the list.
var subList2 = allList.filterNot { subList1.contains(it) }.toMutableList()
but allList and subList are a list of data class defined as :
data class Food(
var name: Name,
var state: State
)
As of now, using this code, the sublist2 can contain food that are in sublist1 because state is different.
I want the filter to be done only on name so the sublist2 only contain food which is not in the sublist1 whatever the state is.
Any idea ?
Try to map the subList1 names:
val subList1Names = subList1.map { it.name }
var subList2 = allList.filterNot { it.name in subList1Names }.toMutableList()
I have 20 arraylists (more to come) containing the same type of data:
var arraylist1 = ArrayList()
var arraylist2 = ArrayList()
var arraylist3 = ArrayList()
And so on.
All the arrays make up a list on different tabs in my app which the user clicks and I can differentiate where the clicks are coming from.
Now I need to clear all arrays except the one where the click came from.
Lets say that I write a function like this
clearAllArraysExceptThis(arraylist1)
Assuming that the click originated from arraylist1. How do I clear all the remaining lists efficiently.
Do I keep a list of the arraylists and get the name loop and compare or is there a better way ?
With kotlin, assuming the following mock lists:
val list1 = arrayListOf(1, 2)
val list2 = arrayListOf(2, 2)
val list3 = arrayListOf(3, 2)
val lists = arrayListOf(list1, list2, list3)
To clear all the lists except the list function parameter, it would look something like this:
fun clearAllArraysExceptThis(listToKeep: List<Int>) {
for (list in lists) {
if (list != listToKeep) list.clear()
}
}
A more functional approach would be:
fun clearAllArraysExceptThis(listToKeep: List<Int>) {
lists.filter { list -> list != listToKeep }
.forEach { list -> list.clear() }
}
If you want to actually remove the lists instead of clearing them, you could use:
fun clearAllArraysExceptThis(listToKeep: List<Int>) {
lists.removeAll { it != listToKeep }
}
You could create an arrayList of arrayLists called "AllArrayListTogether" where you add all yor arrayLists. Then create a function called "clearAllArraysExceptThis" whit one parameter "arrayListSelected".
Then do something like this:
public void clearAllArraysExceptThis(ArrayList arrayListSelected){
for(ArrayList i : AllArrayListTogether){
if(!i.equals(arrayListSelected){
i.clear();
}
}
}
I have a data class like this
data class TestModel(val id: Int, val sentence: String , var isPlaying: Boolean)
And I make a mutableList of that
val list: MutableList<TestModel> = arrayListOf(
TestModel(1,"test",false),
TestModel(2,"test2",false),
TestModel(3,"test3",false))
then make a copy of the list in another object
val list2=list
when I modify list, for example:
list2[0].isPlaying=true
if I check the equality of these two lists
print(list==list2)
the result will be true while data in list is modified
I use this list in Android ListAdapter and while list is as same as old list, the adapter will not understand I have do some modifying.
How I can achieve what I want?
thanks for your response
In that case you are modifying also the data classes of the original list. So if you print both lists you will get the same results:
list: [TestModel(id=1, sentence=test, isPlaying=true), TestModel(id=2,
sentence=test2, isPlaying=false), TestModel(id=3, sentence=test3,
isPlaying=false)]
list2: [TestModel(id=1, sentence=test, isPlaying=true), TestModel(id=2, sentence=test2, isPlaying=false), TestModel(id=3,
sentence=test3, isPlaying=false)]
You need to make a copy of each of the data classes to have the results that you want otherwise you will be referring to the same data classes of the original list and both will have the same data
For that, you can use this function if you want or something to make a copy of those data classes:
fun MutableList<TestModel>.copyOf(): MutableList<TestModel> {
return this.map { it.copy() }.toMutableList()
}
And use it like this:
val list = mutableListOf(
TestModel(1,"test",false),
TestModel(2,"test2",false),
TestModel(3,"test3",false)
)
val list2=list.copyOf()
list2[0].isPlaying=true
println(list==list2)
Hope this helps!
I have two List<CustomObject> and I want to create filtered list which will contain only items from second list, which are not present in first list. Parameter for comparison is objectId which is unique String value.
First, get the IDs of the first list:
val firstListIds = firstList.map { it.objectId }.
Then, filter the second list by checking if the ID is among the IDs of the first list:
val result = secondList.filter { it.objectId !in firstListIds }
I think the following might work well:
val firstListObjectIds = firstList.map { it.objectId }.toSet()
val filteredList = secondList.filter { !firstListObjectIds.contains(it.objectId) }