Search code examples
kotlingenericsgridviewandroid-adapterbaseadapter

How can I iterate through a gridView base adapter to check the text on it?


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
    }
}

Solution

  • 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))
            }
        }