1

I am trying to build something similar to the Android Fragments tutorial: I have a ListView fragment on the left and a fragment that can display an item on the right.

Now I am trying to auto-select the first item in the list as soon (or soon after) it has been loaded through the SimpleCursorAdapter (which I use through the LoaderManager)-- all using the Android support library.

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mAdapter = new SimpleCursorAdapter(/* options */);
    setListAdapter(mAdapter);
}

public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    CursorLoader cl = new CursorLoader(getActivity());
    /* setProjection, etc */
    return cl;
}

public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    mAdapter.swapCursor(data);
}

public void onLoaderReset(Loader<Cursor> loader) {
    mAdapter.swapCursor(null);
}

I have checked various sample implementations and they all seem to be much too simple or simply don't populate the fragment on the right on load but just upon a click.

I needed to implement a onLoadFinished anyway but that is too late for me as I am loading a long list. So I was thinking along the lines of an onItemLoadedListener which I would only use the first time it is called. But nothing like that seems to exist so I am quite stuck.

Thanks for any pointers!

akirk
  • 6,757
  • 2
  • 34
  • 57

1 Answers1

1

what you have against much too simple?

 public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
     data.moveToFirst();
     Bundle b = new Bundle();
     // get info from that first item and put into the bundle
     ...

     ContentFrag f = new ContentFrag();
     f.setArguments(b);
     getSupportFragmentManager().beginTransaction().replace(R.id.contentLayout, f).commit();

     // instantiate and set your adapter
     ...
 }

simple! Just like any other fragment transaction.

Budius
  • 39,391
  • 16
  • 102
  • 144
  • Wow that was fast, thank you. I have updated the question while you posted this, and to put things short, the list is quite long and onLoadFinished is called quite late so I deemed it as not being my solution. – akirk Nov 08 '12 at 17:49
  • Ok I need to take this back. Actually this is called pretty fast even though the list is long. Works. Thanks! – akirk Nov 08 '12 at 17:58