Hello I made a recycler view of «anuncios» for selling houses, and I want to create a filter, by pressing a button inside a popup window, that will filter the data, based on that parameter (for example, there are 4 buttons in this pop-up window (1 bedroom, 2 bedroom, 3+ bedroom, all), and when I press each button, it appears in the recycler view the correspondent data.
I know how to create the pop-pup window, however, I am struggling in finding the best approach to changing the data in my recycler view, depending on the filter button pressed
I am using Retrofit2 in Android, and in the webserver part I am using Notorm and Slim 4
My adapter
class AnuncioAdapter(val anuncios: List<Anuncio>): RecyclerView.Adapter<AnunciosViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AnunciosViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.recyclerline, parent, false)
return AnunciosViewHolder(view)
}
override fun getItemCount(): Int {
return anuncios.size
}
override fun onBindViewHolder(holder: AnunciosViewHolder, position: Int) {
return holder.bind(anuncios[position])
}
}
class AnunciosViewHolder(itemView : View): RecyclerView.ViewHolder(itemView){
private val morada: TextView = itemView.findViewById(R.id.morada)
private val telemovel:TextView = itemView.findViewById(R.id.number)
private val fotografia: ImageView = itemView.findViewById(R.id.image)
fun bind(anuncio: Anuncio) {
morada.text = anuncio.morada
telemovel.text = anuncio.telemovel
Picasso.with(itemView.getContext())
.load(anuncio.fotografia)
.into(fotografia);
}
}
My data class
data class Anuncio(
val address: String,
val mobile: String,
val qrcode: String,
val latitude: Float,
val longitude: Float,
val n_bedrooms: Int,
val n_bathrooms: Int,
val picture: String,
val price: Float,
val furniture: String,
val other_attr: String,
val users_id: Int,
)
My endpoints (i still only have 2. the one that shows all anuncios, and it displays correctly as default, and the one for 1 bedroom, for testing purposes before working on the others)
interface EndPoints {
@GET("/RoomForStudents/api/anuncios")
fun getAnuncios(): Call<List<Anuncio>>
@GET("/RoomForStudents/api/anuncios_t1")
fun getAnuncios_t1(): Call<List<Anuncio>>
My main activity
class ListaAnuncios : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_lista_anuncios)
setTitle(R.string.find)
val recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
val request = ServiceBuilder.buildService(EndPoints::class.java)
val call = request.getAnuncios()
call.enqueue(object : Callback<List<Anuncio>> {
override fun onResponse(call: Call<List<Anuncio>>, response: Response<List<Anuncio>>) {
if (response.isSuccessful){
recyclerView.apply {
setHasFixedSize(true)
layoutManager = LinearLayoutManager(this@ListaAnuncios)
adapter = AnuncioAdapter(response.body()!!)
}
}
}
override fun onFailure(call: Call<List<Anuncio>>, t: Throwable) {
Toast.makeText(this@ListaAnuncios, "${t.message}", Toast.LENGTH_LONG).show()
}
}) }
Edit: I will also add my Service Builder
private val client = OkHttpClient.Builder().build()
private val retrofit = Retrofit.Builder()
.baseUrl("https://tneveda.000webhostapp.com")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build()
fun<T> buildService(service: Class<T>): T {
return retrofit.create(service)
}
I can not understand your data class, because it is not in English. But, in a general sense, Upon click event in pop up window, get current list from your adapter, or you can get the list you have parsed in the parameter of adapter, filter it with required number of bedrooms using
val newList = anuncios.filter{
//your condition for checking the number of bedrooms
}
Then resubmit that newList in adapter and call notifyDataSetChanged() on the adapter.