I am trying to create a small game with the following components:
- 9 cells - in a GridView
- some words (that have to be introduced in the table, each on a cell)
- TextToSpeech - on each cell
Each single cell contains one single word and the user has to choose one of the words displayed below in order to complete the cell (2 words are supposed to be in a cell). Everything works perfectly, the only problem is that the TextToSpeech is being activated once the user chooses the right word, but I would want it to be one step forward. That means, once the user chooses the right word for ONE cell, the TextToSpeech should be automatically activated for the NEXT cell, so once the user has chosen the right word, he should hear the next word from the next cell.
These cells don't have ids, they are in the MainActivity, like this:
private fun setData() {
dataBeanArrayList = ArrayList<CellDataBean>()
val cellDataBean = CellDataBean()
//we store in the var cellDataBean both texts
//according to the class CellDataBean
cellDataBean.text_one = "Hello"
cellDataBean.text_two = "World"
dataBeanArrayList!!.add(cellDataBean)
val cellDataBean1 = CellDataBean()
cellDataBean1.text_one = "Okay"
cellDataBean1.text_two = "Not bad"
If I implement a new text view (which is not preferable), the TextToSpeech only works like I want for the first cell, once you press on this:
starttv.setOnClickListener {
if (count == 0) {
isstart = true
val text: String = dataBeanArrayList[count].text_1
textToSpeech!!.speak(text, TextToSpeech.QUEUE_FLUSH, null)
dataBeanArrayList[count].isSelected = true
cellAdapter!!.notifyDataSetChanged()
}
}
I am trying to implement something like: (where 'mainbacklay' is the ID from the cell)
when(vh.mainbacklay)[position]==0{
// here the text to speech should be enabled for the text from the second cell with the position 1
}
So when the position 2 from the cell is activated, the text to speech should be enabled for the text with the third position and so on.
Could someone help me figure this out, please? Thank you.
CodePudding user response:
I don't have a clear picture of exactly how your app operates, but I think you want something like this:
fun setAnswer(cellNumber: Int, text: String) {
// Update the data with the selected text etc
// Announce the text for cellNumber 1, or a fallback when there is no next cell
val message = dataBeans.getOrNull(cellNumber 1)?.text_1 ?: "You're done!"
textToSpeech.speak(message, TextToSpeech.QUEUE_FLUSH, null)
}
and then whenever your event (a click or whatever) happens for cell n, you call that function. It takes care of handling that event, managing the overall state, and doing anything that needs to happen as a result (like announcing the next cell's contents, or moving to an endgame screen, etc.
Basically you don't want your individual cells / TextView
s / ViewHolder
s managing that overall state and interacting with each other and each other's data. Just hand it off to a central function or component that manages what's going on, and tells the UI what to display. Makes it a lot easier!
Hope that helps, I can only be general without seeing exactly what you're doing