Reputation: 179
What I try to do is that when I open the fragment, the recyclerView shows me the current date and when I click on the button, it changes that date and shows me in the recylerView But I don't know how to do it. As you say when opening the fragment it does not have url until the click is made
formatted is always today
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)
}
}
Upvotes: 1
Views: 47
Reputation: 179
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)
}
Upvotes: 0
Reputation: 26
do you mean formatted is always today after click the button ? I think var formatted has changed after you clicked on btn_button. But if what you mean is formatted on MAIN_URL, it won't change if you put the code outside of setOnClickListener. just place it at the bottom of setOnClickListener, and it will change.
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"
}
Upvotes: 1