Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use startmanagingcursor in fragment?

How can I use startmanagingcursor in fragment? Because I need to pull data from SQLite in Fragment but I cannot use startmanagingcursor in it.

Here is my coding

 @Override  
 public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);
        dbUtil.open();
        Cursor cursor = dbUtil.fetchNews(getArguments().getString("title"));
        getActivity().startManagingCursor(cursor);
        newsTitle = cursor.getString(0);
        dbUtil.close();
 }
like image 650
PPShein Avatar asked Nov 28 '25 06:11

PPShein


1 Answers

Since everyone else seems to want to preach at you and not give you an answer, I'll actually answer your question (then preach :p).

getActivity().startManagingCursor(yourCursor);

It's as simple as that.

As others have noted, it's deprecated and a CursorLoader is recommended for many reasons... keeping data processing off the UI thread being the major one.

But if you truly need/want to use startManagingCursor in a fragment, the above snippet is how you would do it.

Given the further information you have posted, another issue might be where you are trying to call your database from. You should not use the onCreate method in a fragment class (at least for these operations), you should use onActivityCreated. That way you are certain that the activity that controls your fragment has finished setting itself up before you try and use anything associated with it.

like image 81
Barak Avatar answered Nov 30 '25 19:11

Barak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!