gpt4 book ai didi

kotlin - 点击后传递参数

转载 作者:行者123 更新时间:2023-12-02 13:29:11 25 4
gpt4 key购买 nike

我想做的是,当我打开片段时,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)
}
}

最佳答案

您是说单击按钮后,格式化始终是今天吗?我认为您单击btn_button后,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"

}

关于kotlin - 点击后传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62504349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com