【发布时间】:2021-10-21 09:09:44
【问题描述】:
在尝试设置从数据库中提取笔记的函数时,我遇到了这个错误,它不允许我使用 addAll 并抛出以下错误:
未解决的参考。由于接收器类型不匹配,以下候选均不适用: public fun MutableCollection
.addAll(elements: Array ): kotlin.collections 中定义的布尔值 public fun MutableCollection .addAll(elements: Iterable):在 kotlin.collections 中定义的布尔值 public fun MutableCollection .addAll(elements: Sequence): kotlin.collections 中定义的布尔值
此行出现错误:
noteList.addAll(result)
在主活动结束附近的 onPostExecute 函数中
主要活动(错误位置)
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(如果有人要求):
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());
}
}
}
值得注意的是,这个页面上有一个类似的问题,给出的答案是使用可变列表,这对我的情况不起作用,我正在尝试就像 kotlin Land 在这里所说的那样:@987654321 @ 但它不起作用,这个问题有什么提示吗?
【问题讨论】: