有誰知道在片段中更改文本視圖文本值的方法。我已經嘗試過 findviewbyid,但是我相信這在片段中不起作用。有誰知道如何識別文本視圖并更改它的文本值。謝謝
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
}
}
uj5u.com熱心網友回復:
findViewById 也適用于片段,如下所示:
val view = inflater.inflate(R.layout.fragment_home, container, false)
val textView: TextView = view.findViewById(R.id.temp)
textView.text = "My Text"
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/459861.html
上一篇:常用設計模式
下一篇:如何將資料從配接器傳遞到片段?
