addAll Unresolved reference. None of the following candidates is applicable because of receiver type mismatch

Viewed 491

While trying to setup a function to extract notes from the database, I run into this error which dosen't let me use addAll throwing the following error:

Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: public fun MutableCollection<in ???>.addAll(elements: Array<out ???>): Boolean defined in kotlin.collections public fun MutableCollection<in ???>.addAll(elements: Iterable): Boolean defined in kotlin.collections public fun MutableCollection<in ???>.addAll(elements: Sequence): Boolean defined in kotlin.collections

The error ocurs on this line:

noteList.addAll(result)

in the onPostExecute function near the end on the Main Activity

Main Activity (Place of the error)

package com.example.notas.activities

import android.content.AbstractThreadedSyncAdapter
import android.content.Intent
import android.os.AsyncTask
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.example.notas.R
import com.example.notas.adapters.NotesAdapter
import com.example.notas.database.NotesDatabase
import com.example.notas.entities.Note
import java.util.ArrayList




class MainActivity : AppCompatActivity() {

val REQUEST_CODE_ADD_NOTE = 1

lateinit var notesRecyclerView: RecyclerView
lateinit var noteList: List<Note>
lateinit var notesAdapter: NotesAdapter


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val imageAddNoteMain = findViewById(R.id.imageAddNoteMain) as ImageView

    imageAddNoteMain.setOnClickListener {

            startActivityForResult(
                Intent(applicationContext, CreateNoteActivity::class.java), 
REQUEST_CODE_ADD_NOTE
            )

    }

    notesRecyclerView = findViewById(R.id.notesRecyclerView)
    notesRecyclerView.layoutManager = StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)

    noteList = mutableListOf()
    notesAdapter = NotesAdapter(noteList)

    getNotes()
}

fun getNotes(){
    class GetNotesTask : AsyncTask<Void?, Void?, List<Note>>() {
        override fun doInBackground(vararg params: Void?): List<Note>? {
            return NotesDatabase.getDatabase(applicationContext).noteDao().allNotes
        }

        override fun onPostExecute(result: List<Note>?) {
            super.onPostExecute(result)
            if (noteList.size == 0){
                noteList.addAll(result)
                notesAdapter.notifyDataSetChanged()
            } else {
                noteList.add
            }
        }


    }
    GetNotesTask().execute()
}
}

NotesAdapter.java (In case anyone askes for it):

package com.example.notas.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.notas.R;
import com.example.notas.entities.Note;

import java.util.List;

public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.NoteViewHolder> {

private List<Note> notes;

public NotesAdapter(List<Note> notes) {
    this.notes = notes;
}

@NonNull
@Override
public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    return new NoteViewHolder(
            LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.item_container_note,
                    parent,
                    false
            )
    );
}

@Override
public void onBindViewHolder(@NonNull NoteViewHolder holder, int position) {
    holder.setNote(notes.get(position));

}

@Override
public int getItemCount() {
    return notes.size();
}

@Override
public int getItemViewType(int position){
    return position;
}

static class NoteViewHolder extends RecyclerView.ViewHolder{



    TextView textTitle, textSubtitle, textDateTime;

    NoteViewHolder(@NonNull View itemView) {
        super(itemView);
        textTitle = itemView.findViewById(R.id.textTitle);
        textSubtitle = itemView.findViewById(R.id.textSubtitle);
        textDateTime = itemView.findViewById(R.id.textDateTime);
    }

    void setNote(Note note){
        textTitle.setText(note.getTitle());
        if(note.getSubtitle().trim().isEmpty()){
            textSubtitle.setVisibility(View.GONE);
        } else {
            textSubtitle.setText(note.getSubtitle());
        }
        textDateTime.setText(note.getDateTime());
    }
}
}

Its worth to note that there is a similar problem on this page, and the answer given for it was to use mutable lists, which did not work for my case, im trying things out just like kotlin land says here: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/mutable-list-of.html But it does not work, any hints for this issue?

1 Answers

noteList is of type List<Note>, which means its a read only list. you can not add any values to it, it is not mutable. if you want to add/remove values than change its type to MutableList, this can be done as

lateinit var noteList: MutableList<Note>

Now when you add result to noteList, you have to make sure that result is not null, which you can do as

result?.let { noteList.addAll(it) }
Related