使用 sugar ORM 实现回收器视图
Implementing recycler view using sugar ORM
我正在尝试将 activity 上文本视图中的数据显示到片段上的回收器视图上。我有一个适配器 class 来实现适配器方法。但是,当我单击片段时,应用程序关闭并且 .count 方法仍然无法解析。代码附在下面;
public class NotesXFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View noteview = inflater.inflate(R.layout.fragment_notes, container, false);
note_fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getActivity(), AddNote.class));
}
});
RecyclerView recyclerView = (RecyclerView) noteview.findViewById(R.id.note_rv);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
// notesDbAdapter = new NotesDbAdapter(getActivity(),notes);
// recyclerView.setAdapter(notesDbAdapter);
notecount = Note.count(Note.class);
if(notecount>=0){
notes = Note.listAll(Note.class);
notesDbAdapter = new NotesDbAdapter(getActivity(),notes);
recyclerView.setAdapter(notesDbAdapter);
}
return noteview }
}
请检查您是否正确导入了注释 class。此外,您可以尝试使用 SugarRecord class 获取计数,我发现它比使用普通的 classes 更有效。
notecount = SugarRecord.count(Note.class);
请检查您使用的是否是最新版本的库。
此外,每当您更新架构时,请在您的 AndroidManifest.xml
文件中增加您的数据库版本。
<meta-data android:name="VERSION" android:value="2" />