切换ListView适配器后,选择了错误的索引(“变灰”)Kotlin

时间:2019-09-04 18:32:47

标签: android-studio kotlin android-listview

我是新来的。在ListView中长时间单击该项目后,该项目的背景变为灰色。我的列表有很多项目(在屏幕下方展开),因此我必须记住“灰色”项目的索引,然后在自定义适配器中,我必须用那些选定索引的列表检查它们的位置(然后,我决定在其中设置哪种布局getView函数)。

但是现在,当我在ListView中搜索(==过滤)项目时,我的确得到了“旧索引”,因此没有选择具有原始值的项目,但是现在这些项目具有旧索引(但值不同。)我还在过滤时切换了适配器(如何在代码示例中看到以下内容)。

MainActivity.kt onCreate乐趣:

readFile()
        listView.setOnItemLongClickListener { parent, view, index, id ->
            var indexValue = enPairs[index]
            Log.d("david", "Notify long clicked")
            if (index in selectedItems) {
                view.setBackgroundColor(Color.parseColor("#ffffff"))
                selectedItems.remove(index)
                enWords.remove(indexValue.mainWordSplit)
                enDefns.remove(indexValue.defnSplit)
            } else {
                view.setBackgroundColor(Color.parseColor("#e3e3e3"))
                selectedItems.add(index)
                enWords.add(indexValue.mainWordSplit)
                enDefns.add(indexValue.defnSplit)
            }
            true
        }
        var adapter = UserListAdapter(this, enPairs, selectedItems)
        listView?.adapter = adapter

        searchText.addTextChangedListener(object: TextWatcher{
            override fun afterTextChanged(p0: Editable?) {
            }

            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                var searchFilter = enPairs.filter { searchText.text.toString() in it.mainWordSplit }
                var searchAdapter = UserListAdapter(this@MainActivity, searchFilter, selectedItems)
                listView?.adapter = searchAdapter
            }
        })

UserListAdapter.kt getView乐趣:

class UserListAdapter(private var activity: Activity, private var items: List<UserDto>, private var selected: ArrayList<Int>): BaseAdapter() {
    private class ViewHolder(row: View?) {
        var txtName: TextView? = null
        var txtComment: TextView? = null

        init {
            this.txtName = row?.findViewById(R.id.txtName)
            this.txtComment = row?.findViewById(R.id.txtComment)
        }
    }

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val view: View?
        val viewHolder: ViewHolder
        if (position in selected){
            val inflater = activity?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            view = inflater.inflate(R.layout.user_list_row_selected, null)
            viewHolder = ViewHolder(view)
            view?.tag = viewHolder
        } else {
            val inflater = activity?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            view = inflater.inflate(R.layout.user_list_row, null)
            viewHolder = ViewHolder(view)
            view?.tag = viewHolder
        }


        val userDto = items[position]
        viewHolder.txtName?.text = userDto.mainWordSplit
        viewHolder.txtComment?.text = userDto.defnSplit

        return view as View
    }
.
.
.
}

Before searching While searching

有什么想法让我选择的项目保持选中状态吗?

0 个答案:

没有答案