'Kotlin: How can I get a selected list ID?
i want to open all details list that has been selected.
I have two entities : Tag and TagList. Each TagList has a list of tags. So I added a foreign key on entity Tag 'id_tag_list'.
Here's some images of my app :

OpenFragment

I select a list then I clicked on Open which navigates to OpenFragment which display my corresponding list of tags.
I tried this :
ListsFragment :
binding.btnOpen.setOnClickListener {
val direction = ListsFragmentDirections.actionListsFragmentToOpenFragment()
findNavController().navigate(direction)
}
**I created an interface in order to take a TagList : **
interface OnItemSelectedListener<T> {
fun onItemSelected(item: T?)
}
**So ListFragment and ListViewMOdel : **
override fun onItemSelected(item: TagList?) {
viewModel.selected = item
}
class TagListViewModel(private val repository: TagRepository) : ViewModel() {
private val _tagsLists = MutableLiveData<MutableList<TagList>>()
val tagsLists: LiveData<MutableList<TagList>> = _tagsLists
val count = Transformations.map(tagsLists) { it.size }
var selected: TagList? = null
**By the variable 'selected' i can get a TagList but the problem is it obviously doesn't exist on OpenFragment
ReadViewModel **
private val _tags = MutableLiveData<List<Tag>>()
val tags: LiveData<List<Tag>> = _tags
val count = Transformations.map(tags) { it.size }
fun getTags(){
CoroutineScope(Dispatchers.IO).launch {
val tags = repository.getTagsOfList(idList = 11)
_tags.postValue(tags)
}
}
OpenFragment
class OpenFragment(private val viewModel: ReadViewModel) : Fragment(), OnItemSelectedListener<Tag> {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.getTags()
**I put 11 then it obviously display a list of tags with id_tag_list = 11 but not the list which has been selected.
So how can I replace 11 by the current list selected ? **
EDIT: I already tried selected.id.let{} on TagListViewModel but it display nothing because we select nothing on OpenFragment
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
