Filter Weather Forecast By Date throuh OnClickListner android Kotlin

Viewed 9

I want to Filter the Weather Forecast By selecting a Particular Date in a 3 hour Forecast Weather, It is pretty simple now today is sep 19 if i click today then it show only sep 19 forecast weather and other details have to hide, that's all.

WeatherActivity.kt

lateinit var modelMain: WeatherDataModel
private lateinit var forecastList: ArrayList<WeatherDataModel>
private lateinit var forecastAdapter: WeatherForecastAdapter

private fun getWeatherData() {

        val local = LocalDate.now()
        val format = SimpleDateFormat("MMM d", Locale.US)
        val selectedDay = format.parse(local.toString())
        val formattedDate = format.parse(modelMain.dt_txt)

        binding.Btn1.setOnClickListener {
            forecastList.filterList { formattedDate == selectedDay }
            forecastAdapter.notifyDataSetChanged()

        }

    }
private fun dateStamp() {

        val day1 = LocalDateTime.now()
        val day2 = LocalDateTime.now().plusDays(1)
        val day3 = LocalDateTime.now().plusDays(2)
        val day4 = LocalDateTime.now().plusDays(3)
        val day5 = LocalDateTime.now().plusDays(4)
        val day6 = LocalDateTime.now().plusDays(5)
        val day7 = LocalDateTime.now().plusDays(6)

        val sdf = DateTimeFormatter.ofPattern("MMM d")

        val month1 = day1.format(sdf)
        val month2 = day2.format(sdf)
        val month3 = day3.format(sdf)
        val month4 = day4.format(sdf)
        val month5 = day5.format(sdf)
        val month6 = day6.format(sdf)
        val month7 = day7.format(sdf)

        binding.Btn1.text = StringBuilder().append(month1)
        binding.Btn2.text = StringBuilder().append(month2)
        binding.Btn3.text = StringBuilder().append(month3)
        binding.Btn4.text = StringBuilder().append(month4)
        binding.Btn5.text = StringBuilder().append(month5)
        binding.Btn6.text = StringBuilder().append(month6)
        binding.Btn7.text = StringBuilder().append(month7)

    }

WeatherForecastAdapter.kt

    class WeatherForecastAdapter @Inject constructor(
    private val forecastList: ArrayList<WeatherDataModel>,
     val context: Context

    ) :RecyclerView.Adapter<WeatherForecastAdapter.WeatherForecastHolder>() {
   
    class WeatherForecastHolder (itemView: View): RecyclerView.ViewHolder(itemView){

        fun bind(forecastElement : WeatherDataModel) {
            itemView.forecastDegree.text = "${(forecastElement.main.temp - 273.15).toInt()} °C "
            itemView.forecastTime.text =
            SimpleDateFormat("MMM d", Locale.ENGLISH).format(forecastElement.dt*1000L)
            updateUI(forecastElement.weather[0].id)

          }
        }
 override fun onCreateViewHolder(parent: ViewGroup, viewType: Int):WeatherForecastHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.weather_forecast_item,parent,false)
        return WeatherForecastHolder(view)
    }

    override fun onBindViewHolder(holder: WeatherForecastHolder, position: Int) {
      forecastList[position].let {
          holder.bind(forecastElement = it)
      }

    }

    override fun getItemCount(): Int = forecastList.size


}

give me a solution for this i just tried every method

0 Answers
Related