[英]RecyclerView not creating items - ( MVVM + Room + LiveData)
RecyclerView items are not getting created - I'm trying to add a note feature (with MVVM+Room+liveData) to my project.没有创建 RecyclerView 项目 - 我正在尝试向我的项目添加注释功能(使用 MVVM+Room+liveData)。
The same code is fine when used in a new android studio project.相同的代码在新的 android 工作室项目中使用时很好。 implementing it in my existing project is the issue.
在我现有的项目中实施它是问题所在。 (both are with androidX and have the same implementations).
(两者都与 androidX 并具有相同的实现)。
Other recyclerViews in my app are working correctly (not with MVVM)我的应用程序中的其他 recyclerViews 工作正常(不适用于 MVVM)
The recycler view adapter is not getting accessed - I used Log flags.回收器视图适配器没有被访问 - 我使用了日志标志。
The Code source is from here: https://codinginflow.com/tutorials/android/room-viewmodel-livedata-recyclerview-mvvm/part-6-recyclerview-adapter代码源来自这里: https://codinginflow.com/tutorials/android/room-viewmodel-livedata-recyclerview-mvvm/part-6-recyclerview-adapter
Note activity:笔记活动:
public class NoteActivity extends AppCompatActivity {
private NoteViewModel noteViewModel;
ArrayList<Reviews> reviewList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
final NoteAdapter adapter = new NoteAdapter();
recyclerView.setAdapter(adapter);
noteViewModel = ViewModelProviders.of(this).get(NoteViewModel.class);
noteViewModel.getAllNotes().observe(this, new Observer<List<Note>>() {
@Override
public void onChanged(@Nullable List<Note> notes) {
adapter.setNotes(notes);
}
});
}
Note Adapter:注意适配器:
public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteHolder> {
private static final String TAG = "Debug "+NoteAdapter.class.getSimpleName();
private List<Note> notes = new ArrayList<>();
@NonNull
@Override
public NoteHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.note_item, parent, false);
Log.e (TAG, "in onCreateViewHolder");
return new NoteHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull NoteHolder holder, int position) {
Note currentNote = notes.get(position);
holder.textViewTitle.setText(currentNote.getTitle());
holder.textViewDescription.setText(currentNote.getDescription());
holder.textViewPriority.setText(String.valueOf(currentNote.getPriority()));
Log.e (TAG, "in onBindViewHolder");
}
@Override
public int getItemCount() {
return notes.size();
}
public void setNotes(List<Note> notes) {
this.notes = notes;
notifyDataSetChanged();
}
class NoteHolder extends RecyclerView.ViewHolder {
private TextView textViewTitle;
private TextView textViewDescription;
private TextView textViewPriority;
public NoteHolder(View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.text_view_title);
textViewDescription = itemView.findViewById(R.id.text_view_description);
textViewPriority = itemView.findViewById(R.id.text_view_priority);
Log.e (TAG, "in NoteHolder");
}
}
}
the set notes void is the only function getting accessed. set notes void 是唯一被访问的 function。 (using Log Flags)
(使用日志标志)
The entire code with MVVM classes works in a blank studio project.带有 MVVM 类的整个代码在一个空白的工作室项目中工作。
What could be the problem??可能是什么问题呢??
in Log it get this error:在登录它得到这个错误:
E/RecyclerView: No adapter attached; skipping layout
The issue is resolved after logging in MVVM components.该问题在登录 MVVM 组件后得到解决。
I removed the application from my device and installed it again, so the Room database instance was recreated correctly.我从我的设备中删除了该应用程序并再次安装它,因此正确地重新创建了 Room 数据库实例。 Apparently, The instance was empty (I don't know why).
显然,实例是空的(我不知道为什么)。
btw, this log error is not part of the issue (you can get it when everything is okay) see previously answered question in the link below顺便说一句,这个日志错误不是问题的一部分(当一切正常时你可以得到它)在下面的链接中查看之前回答的问题
E/RecyclerView: No adapter attached; skipping layout
recyclerview No adapter attached; recyclerview 没有附加适配器; skipping layout
跳过布局
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.