Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save selected item and position #1

Open
KaeruShi opened this issue Aug 8, 2023 · 1 comment
Open

Save selected item and position #1

KaeruShi opened this issue Aug 8, 2023 · 1 comment

Comments

@KaeruShi
Copy link

KaeruShi commented Aug 8, 2023

How can i save selected item and positon with shared preferences?

thank you

@devAseemSharma
Copy link
Owner

devAseemSharma commented Aug 14, 2023

There are multiple ways, it can be done in one of the callbacks, you will need to add the unique identifier of the selected item (needs to be the field of your Model) in the preference and then later extract it, given this item's list is already stored somewhere.

Another way is to use some kind of Json parser framework like Gson or Moshi to convert the model to jsontString save that string and then when you read that string from preference convert it back to your model.

For this example I am putting all the selected items in listItemSelected ArrayList you can use this list to store it in preference.

In this example there is a interface callback which is provided to rvSelectedSetting touchHelper look for the following code:

 override fun onDrag(v: View?, event: DragEvent): Boolean {
        val state = event.localState as DragData
        when (event.action) {
            DragEvent.ACTION_DRAG_ENTERED -> {
                binding.rvSelectedSetting.setBackgroundResource(R.drawable.green_dashed_box)
            }
            DragEvent.ACTION_DRAG_EXITED -> {
                binding.rvSelectedSetting.setBackgroundResource(R.drawable.dashed_box)
            }
            DragEvent.ACTION_DRAG_ENDED -> {
                binding.rvSelectedSetting.setBackgroundResource(R.drawable.dashed_box)
            }
            DragEvent.ACTION_DROP -> {
                if (listItemSelected.find { it.id == state.item.id } != null) {
                    Toast.makeText(MainActivity@ this, "Setting already exists!", Toast.LENGTH_LONG)
                        .show()
                    return true
                }
  
                listItemSelected.add(0, state.item) // All the selected items getting stored in this list. 
                selectedSettingAdapter.notifyItemRangeInserted(0, 1)
            }
            else -> {}
        }
        return true
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants