我正在嘗試在我的專案中應用視圖系結,我喜歡那個配接器類,但我不明白我將如何應用。任何的想法?
class Center : AppCompatActivity() {
private lateinit var binding: ActivityCenterBinding
var listView: ListView? = null
private var mTitle = arrayOf("Help", "Help2", "Help3")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityCenterBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
listView = binding.help_listView
val adapter = MyAdapter(this, mTitle)
listView!!.adapter = adapter
internal inner class MyAdapter(
context: Center,
private var rTitle: Array<String>,
) : ArrayAdapter<String?>(context, R.layout.row_center, R.id.textView_center, rTitle) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val layoutInflater =
applicationContext.getSystemService(LAYOUT_INFLATER_SERVICE) as LayoutInflater
val binding = ActivityCenterBinding.inflate(layoutInflater, parent, false)
val myTitle = binding.textView_center
myTitle.text = rTitle[position]
return binding.root
}
}
問題是
未解決的參考:textView__center
和
預期變數
對于文本
uj5u.com熱心網友回復:
當您使用視圖系結時,每個布局 XML 類都會生成一個以它命名的“系結”類。因此R.layout.row_center有一個名為RowCenterBinding( 洗掉下劃線,單詞大寫,并以 . 結尾的類Binding。
您可以通過呼叫其靜態方法來創建系結類的實體inflate,如下所示:
// in an Activity
val binding = RowCenterBinding.inflate(layoutInflater)
// in anything else - you pass the thing the View is being inflated into (parent)
// and whether it should actually be added to that parent (no)
val binding = RowCenterBinding.inflate(layoutInflater, parent, false)
現在你有一個RowCenterBinding物件。它有一個root屬性,即您膨脹的視圖層次結構(您需要在問題中的方法末尾回傳getView)。它還為每個View在 XML 檔案中具有 ID 的屬性提供了一個屬性。(這些以相同的方式重命名row_center.xml -> RowCenter。)
因此,您最終會得到一個binding包含您的View```s (binding.root ) and also references to the ones with IDs (binding.textView1 , binding.saveButton`` 等的物件。)就是這樣!
(如果您已經有一個從 XML 檔案擴展而來的視圖層次結構,例如在 中Activity,您可以使用 從中創建系結物件RowCenterBinding.bind(view))
你一直在編輯問題并夸大不同的東西,所以這樣做:
internal inner class MyAdapter(
context: Center,
private var rTitle: Array<String>,
) : ArrayAdapter<String?>(context, R.layout.row_center, R.id.textView_center, rTitle) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val layoutInflater =
applicationContext.getSystemService(LAYOUT_INFLATER_SERVICE) as LayoutInflater
// You're inflating row_center.xml, so RowCenterBinding is the class you want
// Don't specify a type here unless you know what you're doing - this is fine!
val binding = RowCenterBinding.inflate(layoutInflater, parent, false)
// this requires a view in the XML with an android:id of textViewCenter,
// text_view_center, or something like that. Let autocomplete help you here!
val myTitle = binding.textViewCenter
// this should work fine once myTitle is assigned to a TextView
myTitle.text = rTitle[position]
// this method requires an inflated View, so we return the root view
return binding.root
}
}
由于您binding在外部活動中也呼叫了一個變數,因此您可能希望改為呼叫此變數rowBinding或其他變數,以免混淆它們。
(此外,您不需要為 basic 執行任何此操作,ArrayAdapter如果您洗掉該方法,它不就可以作業getView嗎?默認情況下,它應該創建一個視圖并使用您傳遞的建構式引數在 TextView 上設定文本)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/411330.html
標籤:
