【问题标题】:Pass param after click点击后传递参数
【发布时间】:2020-06-21 20:58:37
【问题描述】:

我尝试做的是,当我打开片段时,recyclerView 向我显示当前日期,当我单击按钮时,它会更改该日期并在 recylerView 中显示我但我不知道该怎么做它。 正如您在打开片段时所说,在点击之前它没有网址

格式化总是今天

    class MainProgMovis : AppCompatActivity() {

    var volleyRequest: RequestQueue? = null
    var recipeList: ArrayList<Recipe>? = null
    var recipeAdapter: RecipeListAdapter? = null
    var layoutManager: RecyclerView.LayoutManager? = null
    var MAIN_URL = ""

        @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.guia_list_volley)

        val current = LocalDateTime.now()

        val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
        var formatted = current.format(formatter)
        val format = DateTimeFormatter.ofPattern("EEEE, dd-MM-yyyy")
        val formatt = current.format(format)

        btn_button.setOnClickListener {

            var dt = formatted.toString()

            val sdf = SimpleDateFormat("yyyy-MM-dd")
            val c = Calendar.getInstance()
            c.time = sdf.parse(dt)
            c.add(Calendar.DATE, 1) // number of days to add

            formatted = sdf.format(c.time) // dt is now the new date

            println("Current Date and Time is dt1: $formatted")

            MAIN_URL = "http://www.myurl/$formatted?v=json"

            getRecipe(MAIN_URL)
        }

        //MAIN_URL = "http://www.myurl/$formatted?v=json"

        tv_fech.text = formatt
        recipeList = ArrayList()

        volleyRequest = Volley.newRequestQueue(this)

        getRecipe(MAIN_URL)
    }

    fun getRecipe(url: String) {
        val id_code: String= intent.getStringExtra("id_code")
        val recipeRequest = JsonObjectRequest(
            Request.Method.GET,
            url, Response.Listener {
                    response: JSONObject ->
                try {
                    val resultArray = response.getJSONObject("data")
                    val resultArray2 = resultArray.getJSONObject(id_code)
                    val resultArray3 = resultArray2.getJSONObject("DATOS_CADENA")

                    val resultArray4 = resultArray2.getJSONArray("PROGRAMAS")

                    tv_Text.text = resultArray3["NOMBRE"].toString()

                    println("xxx: " + resultArray4)

                    for (i in 0..resultArray.length() - 1) {
                        var recipeObj = resultArray4.getJSONObject(i)

                        var hora = recipeObj.getString("HORA_INICIO")
                        var programa = recipeObj.getString("TITULO")

                        var recipe = Recipe()
                        recipe.hora = hora
                        recipe.programa = programa

                        recipeList!!.add(recipe)

                        recipeAdapter = RecipeListAdapter(recipeList!!, this)
                        layoutManager = LinearLayoutManager(this)

                        rv_guia_list.layoutManager = layoutManager
                        rv_guia_list.adapter = recipeAdapter

                    }

                    recipeAdapter!!.notifyDataSetChanged()

                }catch (e: JSONException) { e.printStackTrace()}
            },
            Response.ErrorListener {
                    error: VolleyError? ->
                try {
                    Log.d("Error:", error.toString())

                }catch (e: JSONException){e.printStackTrace()}
            })

        volleyRequest!!.add(recipeRequest)
    }
}

【问题讨论】:

    标签: kotlin button click


    【解决方案1】:

    你的意思是点击按钮后总是今天格式化吗?我认为单击 btn_button 后 var formatted 已更改。但是,如果您的意思是在 MAIN_URL 上格式化,如果您将代码放在 setOnClickListener 之外,它不会改变。只要把它放在setOnClickListener的底部,它就会改变。

    tn_button.setOnClickListener {
    
            var dt = formatted.toString()
    
            val sdf = SimpleDateFormat("yyyy-MM-dd")
            val c = Calendar.getInstance()
            c.time = sdf.parse(dt)
            c.add(Calendar.DATE, 1) // number of days to add
    
            formatted = sdf.format(c.time) // dt is now the new date
    
            println("Current Date and Time is dt1: $formatted")
            
            //this will update the formatted value after changed
            var MAIN_URL = "http://www.myurl/$formatted?v=json"
    
        }
    

    【讨论】:

      【解决方案2】:
         The solution:
      
       
      
      btn_button.setOnClickListener {
      
              var dt = formatted.toString()
      
              val sdf = SimpleDateFormat("yyyy-MM-dd")
              val c = Calendar.getInstance()
              c.time = sdf.parse(dt)
              c.add(Calendar.DATE, 1) // number of days to add
      
              formatted = sdf.format(c.time) // dt is now the new date
      
              println("Current Date and Time is dt1: $formatted")
      
              MAIN_URL = "http://www.myurl/$formatted?v=json"
      
              tv_fech.text = formatted
              recipeList = ArrayList()
      
              volleyRequest = Volley.newRequestQueue(this)
      
              getRecipe(MAIN_URL)
          }
      

      【讨论】:

        猜你喜欢
        • 2015-05-08
        • 2022-08-03
        • 1970-01-01
        • 2012-12-15
        • 2021-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多