androidormsugarorm

Implementing recycler view using sugar ORM


i am trying to display data from a textview on an activity onto a recycler view on a fragment. I have an adapter class to implement the adapter methods. however when i click the fragment the app closes and the .count method remains unable to be resolved. the code is attached below;

    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        }

}

Solution

  • Please check if you're correctly importing the Note class. Moreover, you can try to get the count using the SugarRecord class, i find it to work better than using the normal classes.

    notecount = SugarRecord.count(Note.class);
    

    Please check if you're using the most recent version of the library. Additionally, whenever you update your schema, please increment your DB version in your AndroidManifest.xml file.

    <meta-data android:name="VERSION" android:value="2" />