I would like to sort my items in order to put at the beginning chosen elements, I created a small piece of code allowing it which works rather well, but when I update my array in the adapter, the recyclerview is unchanged. I tried to do it with an update function in my adapter, and with the debugger, I see that the passed array is modified but nothing in the view... I also tried to send my array when creating the adapter but nothing changes. More weird, because I also have a filter by alphabetical order which works well.... Anyone have an idea? Thanks
Here's is my sort code which work, I tested it with the debugger:
for (i in 0 until folders.size) {
val isRead: String? = prefNewDir.getString(folders[i].absolutePath, null)
if (isRead != null) {
println("before : " folders[i] "et i :" i)
val old= folders[i]
folders.removeAt(i)
folders.add(0, old)
println("after : " folders[i] "et i :" i)
}
}
Here my update method in my adapter:
fun updateData(data: MutableList<File>) {
println("update before : " items[0])
items = data
println("update after : " items[0])
notifyDataSetChanged()
}
Here's my adapter code:
class DirAdapter(
private val context: Context,
private var items: MutableList<File>,
) : RecyclerView.Adapter<DirAdapter.DirViewHolder>() {
private val prefNewDir: SharedPreferences = context.getSharedPreferences("new_files", Context.MODE_PRIVATE)
class DirViewHolder(view : View) : RecyclerView.ViewHolder(view) {
val Dir_name = view.findViewById<TextView>(R.id.file_name)
val DirIsRead = view.findViewById<ImageView>(R.id.file_new)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType : Int):DirViewHolder{
val view : View = LayoutInflater.from(parent.context) //Mode dossier
.inflate(R.layout.folder_element, parent, false)
return DirViewHolder(view)
}
fun updateData(data: MutableList<File>) {
println("update data : " items[0])
items = data
println("update data : " items[0])
notifyDataSetChanged()
}
override fun onBindViewHolder(holder: DirViewHolder, position: Int) {
val currentDir = items[position]
holder.Dir_name.text = currentDir.name
val isRead: String? = prefNewDir.getString(currentDir.absolutePath, null) // On recupere si oui ou non le fichier est lu
if (isRead!=null) {
holder.DirIsRead.visibility = View.VISIBLE
} else
holder.DirIsRead.visibility = View.GONE
holder.itemView.setOnClickListener {
val intent = Intent(context, FilesActivity::class.java)
intent.putExtra("path",currentDir.absolutePath)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(intent)
}
holder.itemView.setOnLongClickListener {
val popupMenu = PopupMenu(context, it)
if (isRead != null) { //Popup seulement sur un element
popupMenu.menu.add("Marquer comme lu")
}
popupMenu.setOnMenuItemClickListener {
if (it.title == "Marquer comme lu") {
putFolderAsRead(currentDir)
holder.DirIsRead.visibility = View.GONE
}
true
}
popupMenu.show()
true
}
}
private fun putFolderAsRead(folder: File) {
val editor: SharedPreferences.Editor = prefNewDir.edit()
editor.remove(folder.absolutePath)
//Dossier courant
val folderList = folder.listFiles()
for (dListItem in folderList) {
if(dListItem.isDirectory) //Si un dossier est present on reboucle dessus
putFolderAsRead(dListItem)
editor.remove(dListItem.absolutePath)
println(dListItem.absolutePath " Marque comme lu !")
}
editor.apply()
}
override fun getItemCount() = items.size
}
CodePudding user response:
First of all for storing data in shared preferences, I'd prefer you to use my library here. You can refer the docs to see how it works. Also, I'd prefer you to use a data class
instead of the File class. That won't give you what you want. Since, you already show the files without sort, you just need some modifications:
Make a data class like this:
data class MyFile(var file: File, var isNew: Boolean = false)
Now, the sorting part will be like this:
var folders = getFolders(Environment.getExternalStorageDirectory()) for (i in 0 until folders.size) { val isRead: String? = prefNewDir.getString(folders[i].absolutePath, null) folders.get(i).isNew = if(isRead == null) false else true }
Now also, you might want to get the folder without sorting and you might be a bit confused, so, here is the code for getting the folders
fun getFolders(location:File) : ArrayList<MyFile>(){ val files = location.listFile() var foldersList = ArrayList<MyFile>() for (file in files) { if(file.isDirectory){ foldersList.add(MyFile(file)) } } }
This will do the task for you.