如何通过GridView Base适配器迭代以检查其上的文本?

发布于 2025-01-23 13:23:16 字数 1947 浏览 2 评论 0原文

我已经尝试了不同的方法,我一直坚持尝试数周。 我还尝试在适配器中添加代码,我考虑过使用SQLite,但我只是不知道该怎么做。

我只想检查一个条件,如果该字符串存在于GridView, boolean = true

我可以在活动中做类似的事情吗?

        for (i in 0 until arrayList!!.size){
        WordViewAdapter(this,arrayList).getView(i, convertView = null, parent = null).word.text
    }
}

这是我的网格查看:

”在此处输入图像描述”

这是我的适配器:

private lateinit var mTTS : TextToSpeech
class WordViewAdapter(var ctx: Context, var array: ArrayList<Word_Item>?) : BaseAdapter() {
    override fun getCount(): Int {
        return array!!.size
    }

    override fun getItem(position: Int): Any {
        return array!![position]
    }
    override fun getItemId(position: Int): Long {
        return position.toLong()
    }
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var view : View = View.inflate(ctx, R.layout.grid_item_list,null)
        var word:TextView = view.findViewById(R.id.tx_word)
        var translation:TextView = view.findViewById(R.id.tx_transl)
        var wordItem : Word_Item = array!![position]
        word.text = wordItem.word
        translation.text = wordItem.transl
        Translate(ctx).question(word,translation)

        view.setOnClickListener{
            mTTS = TextToSpeech(ctx, TextToSpeech.OnInitListener { status ->
                if (status != TextToSpeech.ERROR){
                    mTTS.setLanguage(Locale.US)
                    mTTS.setSpeechRate(0.7F)
                    mTTS.setPitch(0.7F)
                    mTTS.speak(word.text.toString(),TextToSpeech.QUEUE_FLUSH,null,null)
                }
            })
        }
        return view
    }
}

I have tried different ways, I've been stuck trying to do it for weeks.
I also tried to add the code in the adapter, I thought about using sqlite, but I just can't figure how to do it.

I just want to check a condition, if this string exists somewhere in gridview, boolean = true

Can I do something like that in the activity?

        for (i in 0 until arrayList!!.size){
        WordViewAdapter(this,arrayList).getView(i, convertView = null, parent = null).word.text
    }
}

This is my grid view:

enter image description here

This is my Adapter:

private lateinit var mTTS : TextToSpeech
class WordViewAdapter(var ctx: Context, var array: ArrayList<Word_Item>?) : BaseAdapter() {
    override fun getCount(): Int {
        return array!!.size
    }

    override fun getItem(position: Int): Any {
        return array!![position]
    }
    override fun getItemId(position: Int): Long {
        return position.toLong()
    }
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var view : View = View.inflate(ctx, R.layout.grid_item_list,null)
        var word:TextView = view.findViewById(R.id.tx_word)
        var translation:TextView = view.findViewById(R.id.tx_transl)
        var wordItem : Word_Item = array!![position]
        word.text = wordItem.word
        translation.text = wordItem.transl
        Translate(ctx).question(word,translation)

        view.setOnClickListener{
            mTTS = TextToSpeech(ctx, TextToSpeech.OnInitListener { status ->
                if (status != TextToSpeech.ERROR){
                    mTTS.setLanguage(Locale.US)
                    mTTS.setSpeechRate(0.7F)
                    mTTS.setPitch(0.7F)
                    mTTS.speak(word.text.toString(),TextToSpeech.QUEUE_FLUSH,null,null)
                }
            })
        }
        return view
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云之铃。 2025-01-30 13:23:16

好吧,我做到了。我会在这里发布它,也许对某人有帮助。

我已经创建了另一个列表:

var faladas = mutableListOf<String>()

和一个函数,该函数检查是否相等,例如,如果可以,请发送到适配器,然后对其进行更新:

   private fun checkPalavra(str: String) {
        var flip : Boolean = str.lowercase().trim().contains(binding.txPronunciar.text.toString().lowercase().trim().replace("!","").replace(".","").replace("?","").replace(",","") )
        if (flip){
            faladas.add(str.lowercase())
            wordAdapter?.notifyDataSetChanged()
            gridView?.invalidateViews()
            gridView?.adapter = WordViewAdapter(this,arrayList,faladas)...

in适配器,我只是添加:

  for (i in 0 until faladas.size){
        if (faladas[i].lowercase() == word.text.toString().lowercase()){
            view.setBackgroundColor(ctx.resources.getColor(R.color.green))
        }
    }

Well guys, I did it. And I will post it here, maybe it help someone.

I've created another list:

var faladas = mutableListOf<String>()

And a function that check if it is equal, like that, if OK, then send to adapter, and update it:

   private fun checkPalavra(str: String) {
        var flip : Boolean = str.lowercase().trim().contains(binding.txPronunciar.text.toString().lowercase().trim().replace("!","").replace(".","").replace("?","").replace(",","") )
        if (flip){
            faladas.add(str.lowercase())
            wordAdapter?.notifyDataSetChanged()
            gridView?.invalidateViews()
            gridView?.adapter = WordViewAdapter(this,arrayList,faladas)...

In the adapter, I just add it:

  for (i in 0 until faladas.size){
        if (faladas[i].lowercase() == word.text.toString().lowercase()){
            view.setBackgroundColor(ctx.resources.getColor(R.color.green))
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文