Я использую FirebaseRecyclerAdapter
для отображения посещаемости пользователей. Я хочу отфильтровать данные локально. поэтому, если пользователь нажал кнопку x в searchview, исходные результаты должны отобразиться снова.
Я пытался реализовать серверный фильтр какое-то время, но не смог, потому что adapter.notifyDataSetChanged
не работает .
вот мой адаптер
package iti.intake40.mawgood_admin.reports.per_day
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.firebase.ui.firestore.FirestoreRecyclerAdapter
import com.firebase.ui.firestore.FirestoreRecyclerOptions
import iti.intake40.mawgood_admin.R
import iti.intake40.mawgood_admin.core.FirebaseConstants
import iti.intake40.mawgood_admin.reports.Report
import iti.intake40.mawgood_admin.reports.per_user.ReportPerUserActivity
import kotlinx.android.synthetic.main.reports_item.view.*
class ReportPerDayAdapter(options: FirestoreRecyclerOptions<Report>) :
FirestoreRecyclerAdapter<Report,
ReportPerDayAdapter.ReportsViewHolder>(options) {
private lateinit var context: Context
class ReportsViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ReportsViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.reports_item, parent, false)
return ReportsViewHolder(
view
)
}
override fun onBindViewHolder(holder: ReportsViewHolder, position: Int, model: Report) {
holder.itemView.report_item_name.text = model.userName
holder.itemView.report_item_email.text = model.userEmail
holder.itemView.report_item_team.text = model.attend
holder.itemView.report_item_points.text = model.lostPoints.toString()
holder.itemView.setOnClickListener { v ->
val intent = Intent(context, ReportPerUserActivity::class.java)
intent.putExtra(FirebaseConstants.USER_ID, model.uid)
context.startActivity(intent)
}
}
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
context = recyclerView.context
}
}
Мой код фрагмента
package iti.intake40.mawgood_admin.reports.per_day
import android.os.Bundle
import android.util.Log
import android.view.*
import androidx.appcompat.widget.SearchView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.firebase.ui.firestore.FirestoreRecyclerOptions
import iti.intake40.mawgood_admin.R
import iti.intake40.mawgood_admin.reports.Report
import kotlinx.android.synthetic.main.fragment_reports.*
class ReportsFragment : Fragment(), ReportPerDayContract.IView {
private val TAG = ReportsFragment::class.java.simpleName
private lateinit var presenter: ReportPerDayPresenter
private lateinit var adapter: ReportPerDayAdapter
private lateinit var options: FirestoreRecyclerOptions<Report>
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.fragment_reports, container, false)
presenter =
ReportPerDayPresenter(
context!!,
this
)
return root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
options = presenter.getAllUsersAttendancePerDay("1590703200")
setUpRecyclerView()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
}
override fun onStart() {
super.onStart()
activity!!.title = "Reports"
adapter.startListening()
}
override fun onStop() {
super.onStop()
adapter.stopListening()
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.user_search_menu, menu)
val searchItem = menu.findItem(R.id.action_search_user)
val searchView: SearchView = searchItem.actionView as SearchView
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
Log.d(TAG, query)
options = presenter.queryResultsPerUser(query!!)
setUpRecyclerView()
return false
}
override fun onQueryTextChange(newText: String?): Boolean {
return false
}
})
super.onCreateOptionsMenu(menu, inflater)
}
override fun displayNoData() {
// empty_iv.visibility = View.VISIBLE
// empty_tv.visibility = View.VISIBLE
}
override fun hideNoData() {
// empty_iv.visibility = View.INVISIBLE
// empty_tv.visibility = View.INVISIBLE
}
override fun updateDataAdapter() {
adapter.notifyDataSetChanged()
}
private fun setUpRecyclerView() {
adapter =
ReportPerDayAdapter(options)
report_rv.setHasFixedSize(true)
report_rv.layoutManager = LinearLayoutManager(context)
report_rv.adapter = adapter
adapter.notifyDataSetChanged()
}
}