Home > Software design >  I am doing the Kotlin android basics course and I'm getting expecting member declaration on a s
I am doing the Kotlin android basics course and I'm getting expecting member declaration on a s

Time:04-21

Hi I'm new to kotlin and I'm working on the words dictionary app I accidentally deleted a line of code but managed to put it back in and now for some reason it's not working. The error is expecting member declaration. I'm guessing it's linked to line 118 of the code expecting top level declaration, it's just a curly bracket. PLease help

/**
 * Adapter for the [RecyclerView] in [DetailActivity].
 */
class WordAdapter(private val letterId: String, context: Context) :
    RecyclerView.Adapter<WordAdapter.WordViewHolder>() {

    private val filteredWords: List<String>

    init {
        // Retrieve the list of words from res/values/arrays.xml
        val words = context.resources.getStringArray(R.array.words).toList()

        filteredWords = words
            // Returns items in a collection if the conditional clause is true,
            // in this case if an item starts with the given letter,
            // ignoring UPPERCASE or lowercase.
            .filter { it.startsWith(letterId, ignoreCase = true) }
            // Returns a collection that it has shuffled in place
            .shuffled()
            // Returns the first n items as a [List]
            .take(5)
            // Returns a sorted version of that [List]
            .sorted()
    }

    class WordViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
        val button = view.findViewById<Button>(R.id.button_item)
    }

    override fun getItemCount(): Int = filteredWords.size

    /**
     * Creates new views with R.layout.item_view as its template
     */
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WordViewHolder {
        val layout = LayoutInflater
            .from(parent.context)
            .inflate(R.layout.item_view, parent, false)

        // Setup custom accessibility delegate to set the text read
        layout.accessibilityDelegate = Accessibility

        return WordViewHolder(layout)
    }

    /**
     * Replaces the content of an existing view with new data
     */
    override fun onBindViewHolder(holder: WordViewHolder, position: Int) {
        holder.button.setOnClickListener {
            val queryUrl: Uri = **Uri.parse("${DetailActivity.SEARCH_PREFIX}${item}")<---Unresolved reference item**
            val intent = Intent(Intent.ACTION_VIEW, queryUrl)
            **context.startActivity(intent)<---Unresolved reference context**

        }


    }
        val item = filteredWords[position]<--Unresolved reference position
        // Needed to call startActivity
        val context = holder.view.context <--Unresolved reference holder

        // Set the text of the WordViewHolder
        **holder.button.text = item**  **<---This line is the error Expecting member declaration**

    }
    // Setup custom accessibility delegate to set the text read with
    // an accessibility service
    companion object Accessibility : View.AccessibilityDelegate() {
        @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
        override fun onInitializeAccessibilityNodeInfo(
            host: View?,
            info: AccessibilityNodeInfo?
        ) {
            super.onInitializeAccessibilityNodeInfo(host, info)
            // With `null` as the second argument to [AccessibilityAction], the
            // accessibility service announces "double tap to activate".
            // If a custom string is provided,
            // it announces "double tap to <custom string>".
            val customString = host?.context?.getString(R.string.look_up_word)
            val customClick =
                AccessibilityNodeInfo.AccessibilityAction(
                    AccessibilityNodeInfo.ACTION_CLICK,
                    customString
                )
            info?.addAction(customClick)
        }
    }
}          <------Here is line 118 Expecting a top level declaration

CodePudding user response:

delete the '}' from top of val item declaration

  • Related