Does anyone know of a way to change the value of a textview text, whilst in a fragment. I have tried findviewbyid, however i believe that this does not work whilst in a fragement. Does anyone have an idea on how to identify a textview and change the text value of it. Thanks
class Home : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_home, container, false)
val queue = Volley.newRequestQueue(view.context)
val url = "https://io.adafruit.com/api/v2/alex9301/feeds/test-data/data" //URL
var urlStuff = ""
val stringRequest = StringRequest(Request.Method.GET, url,
{ response ->
// Display the first 500 characters of the response string.
urlStuff = response
val jsonArray = JSONTokener(urlStuff).nextValue() as JSONArray
val id = jsonArray.getJSONObject(0).getString("value") // Get data
Log.i("Val: ", id)
//Updating info on app
val textView = view.findViewById(R.id.temp)
textView.text = "My Text"
},
{ Log.i("b", "That didn't work!") })
queue.add(stringRequest)
return view
}
}
CodePudding user response:
findViewById also works in a fragment, like this:
val view = inflater.inflate(R.layout.fragment_home, container, false)
val textView: TextView = view.findViewById(R.id.temp)
textView.text = "My Text"