elirigobeli elirigobeli - 9 days ago 6
Android Question

Show Progress Indicator before fill listview using CursorLoader

What better way to show a progress indicator while my listview is not filled with data of database?

I found some examples of how to do this, using assynctask, but in my case, I am using

Loader
/
CursorLoader
.

public class TestActivity extends SherlockFragmentActivity implements
LoaderCallbacks<Cursor> {

SimpleCursorAdapter mAdapter;
ListView mListView;

private static final String[] UI_BINDING_FROM = new String[] {
TestDBAdapter.KEY_NAME, TestDBAdapter.KEY_ICON };

private static final int[] UI_BINDING_TO = new int[] { R.id.text, R.id.icon };

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_Test);

mListView = (ListView) findViewById(R.id.listview);

mAdapter = new TestAdapter(getApplicationContext(),
R.layout.list_item_Test, null, UI_BINDING_FROM,
UI_BINDING_TO, 0);

mListView.setAdapter(mAdapter);

getSupportLoaderManager().initLoader(0, null, this);

}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {


Uri uri = ContentProviderTest.CONTENT_URI;

CursorLoader cursorLoader = new CursorLoader(
this,
uri,
null,
null,
null,
null);

return cursorLoader;

}

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

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


A possible solution, would be to create a new class extending
ListFragment
and use the
setListShown()
method , but I do not know if it is the best way to solve this issue.

Thanks

Answer

Add a small ProgressBar to your layout file, with its visibility set to "gone". Just before you create your CursorLoader, set its visibility to "visible". It will appear. In onLoadFinished, set the ProgressBar visibility to "gone". It will disappear, and your ListView will load.

I'd use the small style progressbar. To learn more about this, see the reference docs for android.widget.ProgressBar.

BTW, visibility is controlled with View.setVisibility.