Kotlin 单击后传递参数

Kotlin 单击后传递参数,kotlin,button,click,Kotlin,Button,Click,我试图做的是,当我打开片段时,recyclerView会显示当前日期,当我点击按钮时,它会更改该日期,并在recylerView中显示我,但我不知道如何操作。 正如您在打开片段时所说,在单击之前,它没有url 今天总是这样 class MainProgMovis : AppCompatActivity() { var volleyRequest: RequestQueue? = null var recipeList: ArrayList<Recipe>?

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

今天总是这样

    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)
    }
}
类MainProgMovis:AppCompatActivity(){ var-volleyRequest:RequestQueue?=null 变量recipeList:ArrayList?=null 变量recipeAdapter:RecipeListAdapter?=null var layoutManager:RecyclerView.layoutManager?=null var MAIN_URL=“” @SuppressLint(“SetTextI18n”) 重写创建时的乐趣(savedInstanceState:Bundle?){ super.onCreate(savedInstanceState) setContentView(R.layout.guia\u列表\u截取) val current=LocalDateTime.now() val formatter=DateTimeFormatter.of模式(“yyyy-MM-dd”) var formatted=current.format(格式化程序) val格式=模式的日期时间格式(“EEEE,dd-MM-yyyy”) val format=current.format(格式) btn_button.setOnClickListener{ var dt=格式化的.toString() val sdf=简化格式(“yyyy-MM-dd”) val c=Calendar.getInstance() c、 时间=sdf.parse(dt) c、 add(Calendar.DATE,1)//要添加的天数 formatted=sdf.format(c.time)//dt现在是新日期 println(“当前日期和时间为dt1:$formatted”) 主URL=”http://www.myurl/$formatted?v=json“ getRecipe(主URL) } //主URL=”http://www.myurl/$formatted?v=json“ tv\u fech.text=format recipeList=ArrayList() volleyRequest=Volley.newRequestQueue(此) getRecipe(主URL) } 有趣的getRecipe(url:String){ val id_代码:String=intent.getStringExtra(“id_代码”) val recipeRequest=JsonObjectRequest( Request.Method.GET, url,Response.Listener{ 答复:JSONObject-> 试一试{ val resultArray=response.getJSONObject(“数据”) val resultArray 2=resultArray.getJSONObject(id_代码) val resultarray 3=resultarray 2.getJSONObject(“DATOS_CADENA”) val resultarray 4=resultarray 2.getJSONArray(“PROGRAMAS”) tv_Text.Text=resultaray3[“NOMBRE”].toString() println(“xxx:+resultary4) 对于(0..resultArray.length()中的i-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(配方) recipeAdapter=RecipeListAdapter(recipeList!!,this) layoutManager=LinearLayoutManager(此) rv_guia_list.layoutManager=layoutManager rv_guia_list.adapter=recipeAdapter } recipeAdapter!!.notifyDataSetChanged() }catch(e:JSONException){e.printStackTrace()} }, Response.ErrorListener{ 错误:截击错误?-> 试一试{ Log.d(“错误:,错误.toString()) }catch(e:JSONException){e.printStackTrace()} }) 截击请求!!.add(recipeRequest) } }
您的意思是,在单击按钮后,总是在今天进行格式化吗?我认为在点击btn_按钮后,var格式已经改变。但是,如果您的意思是在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"

    }
   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)
    }