Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send cursor from android activity to fragment

Actually I know how to send extra values from activity to fragment, but I need to send contacts cursor to fragment and after that I am passing that cursor to cursor adapter and showing data in listview.

like image 761
skyshine Avatar asked Oct 20 '22 17:10

skyshine


1 Answers

There are a number of ways to accomplish what you are trying to do. Only some of them require you to possess the Cursor prior to creating the Fragment.


You could create a method that creates your fragment and pass in the cursor then:

public class YourFragment extends Fragment {

    private Cursor mCursor;

    public static YourFragment createYourFragmentWithCursor( Cursor cursor ) {
        YourFragment fragment = new YourFragment();
        fragment.setCursor( cursor );
        return fragment;
    }

    @Override
    public void onViewCreated (View view, Bundle savedInstanceState) {
        ListView listView = (ListView) findViewById( R.id.yourListView );
        listView.setAdapter( new CursorAdapter( getActivity(), getCursor() );
    }

    protected Cursor getCursor() {
        return mCursor;
    }

    private Cursor setCursor( Cursor cursor ) {
        mCursor = cursor;
    }
}

You could pass the cursor from the activity to a fragment it controls would be to have the activity implement an interface containing a method that returns the cursor. Then, in your fragment you can get a reference to that interface.

For example:

public interface CursorProvidingInterface {
    Cursor getCursor();
}


public class YourActivity extends Activity implements CursorProvidingInterface {
    ...
    @Override
    public Cursor getCursor() {
        Cursor cursor = whateverYouDoToAcquireYourCursor();
        return cursor;
    }
    ...
}



public class YourFragment extends Fragment {

    private CursorProvidingInterface cursorProvidingInterface;

    @Override
    public void onAttach( Activity activity ) {
        try {
            cursorProvidingInterface = (CursorProvidingInterface) activity;
        }
        catch (ClassCastException e) {
            throw new RuntimeException( activity.getClass().getName() + " must implement " + CursorProvidingInterface.class.getName() );
        }
    }

    @Override
    public void onViewCreated (View view, Bundle savedInstanceState) {
        ListView listView = (ListView) findViewById( R.id.yourListView );
        listView.setAdapter( new CursorAdapter( getActivity(), cursorProvidingInterface.getCursor() );
    }
}

Depending on your situation the above strategies may not be the best to choose.

Assuming your cursor comes from a database I would suggest that you simply acquire the cursor from the database when your fragment is created. You could pass in any query parameters you might need as arguments to the fragment if necessary.

In this case, I prefer to use some form of dependency injection such as RoboGuice and create a @Singleton class that will handle your database transactions that you can then @Inject and then call upon when needed.


You might consider implementing ContentProvider and using it to get the cursor you need. This could be especially helpful if the data you are showing from your Cursor is likely to change frequently.


These last two cases are the more robust ways to go about passing data around in a Cursor and I suggest you familiarize yourself with them. A search for tutorials on these methods will yield much better examples than I will provide here.

like image 103
dm78 Avatar answered Oct 23 '22 21:10

dm78