android RecyclerView.Adapter对象在扩展类后无法正常工作

o4hqfura  于 2023-02-06  发布在  Android
关注(0)|答案(1)|浏览(121)

我想让这个搜索类在数据库中搜索用户,我把一些我经常使用的函数放到一个抽象类中,比如扩展了“AppCompactActivity”的“Abs”
除了扩展的类之外,我什么也没做,它可以完美地与旧类一起工作,但是当我将它更改为更新的类时,它会给出一个错误
这是更新的类

class SearchActivity : AbsBottom(R.layout.activity_search, R.id.bottomNav, R.id.search_nav) {

    private var user = mutableListOf(UserSearch()) 
        private var userAdapter = UserViewHolder()
        lateinit var binding: ActivitySearchBinding

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            binding = ActivitySearchBinding.inflate(layoutInflater)
            //setContentView(binding.root)
            //layout
            binding.recyclerProfile.setHasFixedSize(true)
            val llm = LinearLayoutManager(this)
            llm.orientation = LinearLayoutManager.VERTICAL
            binding.recyclerProfile.layoutManager = llm
            
            //region Adapter Setup
            binding.recyclerProfile.adapter = userAdapter
            binding.searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
                override fun onQueryTextSubmit(query: String?): Boolean {
            //do stuff
                }

                override fun onQueryTextChange(newText: String?): Boolean {
            //do other stuff
                }

            })
            //endregion
    }
}

这是原件

class SearchActivity : AppCompatActivity() {

        private var user = mutableListOf(UserSearch())
        private var userAdapter = UserViewHolder()
        lateinit var binding: ActivitySearchBinding

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            binding = ActivitySearchBinding.inflate(layoutInflater)
            setContentView(binding.root)

            bottomNavigation()
            //layout
            binding.recyclerProfile.setHasFixedSize(true)
            val llm = LinearLayoutManager(this)
            llm.orientation = LinearLayoutManager.VERTICAL

            binding.recyclerProfile.layoutManager = llm
            // userAdapter.setUsers(user)

            //region Adapter Setup
            binding.recyclerProfile.adapter = userAdapter
            binding.searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
                override fun onQueryTextSubmit(query: String?): Boolean {
            //do stuff
                }

                override fun onQueryTextChange(newText: String?): Boolean {
            //do other stuff
                }

            })
            //endregion
    }
}

这是抽象类的外观

abstract class AbsBottom(val idC: Int, val idB: Int, val ac: Int) : AppCompatActivity() {
    protected lateinit var bottomNav : BottomNavigationView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(idC)
        bottomNav = findViewById(idB)
        bottomNav.selectedItemId = ac
    
    //etc
    }
}

这个错误没有说明太多,它说它看不到userAdapter,因此它没有加载搜索到的用户列表

nlejzf6q

nlejzf6q1#

已修复此问题,对行进行注解

//setContentView(binding.root)

把装订线弄坏了

相关问题