我是一个全新的使用Kotlin的人,我试图创建我的第一个应用程序,有很多测试和错误(并且一直工作到现在)。
我的问题是,我已经创建了一个recyclerview,有行从文件在firestore(一切正确),在每行一个按钮,以删除该文件从firestore(工作顺利),但点击按钮后,列表没有更新,所以我仍然看到我刚刚删除的文件...
让我与你分享,请帮助我确定错误,因为我这样做的房间和名单是刷新accordingly。
我的碎片:
class SharingFragment : Fragment() {
override fun onViewStateRestored(savedInstanceState: Bundle?) {
super.onViewStateRestored(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val binding: FragmentSharesBinding = DataBindingUtil.inflate(
inflater, R.layout.fragment_shares, container, false)
val application = requireNotNull(this.activity).application
val viewModelFactory = SharingViewModelFactory(application)
val sharingViewModel = ViewModelProvider(this,viewModelFactory).get(
SharingViewModel::class.java)
sharingViewModel.getSharedWithMe().observe(this , Observer { list->
// set the layout manager and the adapter for the recycler view
binding.listaRecyclerView.layoutManager = LinearLayoutManager(application)
binding.listaRecyclerView.adapter = InsuranceAdapter(context!!,list)
})
binding.sharingViewModel = sharingViewModel
val root: View = binding.root
return root
}
}
我的视图模型(SharingViewModel):
class SharingViewModel(
application: Application
) : AndroidViewModel(application)
{
var listReturn: MutableLiveData<List<String>>
init {
listReturn = MutableLiveData(listOf())
}
fun getSharedWithMe() : MutableLiveData<List<String>> {
var auth = Firebase.auth
val storage = Firebase.storage
var storageRef = storage.reference
val databaseRef = storageRef.child("shared/${auth!!.currentUser!!.email}")
var listFile = databaseRef.listAll()
listFile.addOnSuccessListener { (items, prefixes) ->
var list: MutableList<String> = mutableListOf()
items.forEach { item ->
list.add(item.name)
}
listReturn.value = listReturn.value?.plus(list)
}
.addOnFailureListener {
// Uh-oh, an error occurred!
}
//Wait to receive the listall
while(listFile.isComplete==false) {Thread.sleep(1000)}
return listReturn
}
fun deleteSharedWithMe(file: String) {
var auth = Firebase.auth
val storage = Firebase.storage
var storageRef = storage.reference
val fileRef =
storageRef.child("shared/${auth!!.currentUser!!.email}/${file}")
fileRef.delete()
listReturn.value = listReturn.value!!.toMutableList().apply {
remove(file)
}.toList()
}
}
我的适配器:
class InsuranceAdapter(val context : Context,val list : List<String>) : RecyclerView.Adapter<InsuranceAdapter.ViewHolder>() {
// Inner ViewHolder class
class ViewHolder(val binding : SharingListBinding) : RecyclerView.ViewHolder(binding.root){}
// function to inflate the layout for each contact and create a new ViewHolder instance
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(
SharingListBinding.inflate(LayoutInflater.from(parent.context),parent,false)
)
}
// function to bind the data to the view elements of the ViewHolder
@RequiresApi(Build.VERSION_CODES.O)
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.binding.fileTextview.text = list[position]
// delete button onClickListener to delete the
// file from firestore and notify the
// adapter of the change
holder.binding.deleteButton.setOnClickListener{
val application = requireNotNull(Application())
val sharingViewModel = SharingViewModel(Application())
//TODO borrar de Firebase
sharingViewModel.deleteSharedWithMe(list[position])
// dao.delete(list[position]) //this was working when I was using Room and deleting from the DB was refreshing the list
notifyItemChanged(position)
notifyDataSetChanged()
}
}
override fun getItemCount(): Int {
return list.size
}
}
所以,我所遵循的是使用viewmodel内部的函数来删除文件,但我不知道为什么列表不刷新,有什么想法吗?
1条答案
按热度按时间gzszwxb41#
列表不刷新的原因是您正在InsuranceAdapter类中创建
SharingViewModel
的新示例,该示例与SharingFragment观察到的示例不同在你的片段中:
在适配器中