我想做的是,当我打开片段时,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)
}
}
答案 0 :(得分:1)
您是说单击按钮后,格式化始终是今天?我认为您单击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"
}
答案 1 :(得分:0)
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)
}