Show progress bar before populating a list with CursorLoader

What's the best way to show a progress indicator until my list is populated with database data?

I found some examples of how to do this using assynctask, but in my case I use 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 that extends ListFragmentand use the method setListShown(), but I do not know if this is the best way to solve this problem.

thank

+5
source share
3 answers

ProgressBar , , "". CursorLoader "". . onLoadFinished ProgressBar "". , ​​ ListView .

. , . android.widget.ProgressBar.

BTW, View.setVisibility.

+13

: onLoadFinished() :

if (cursor.getCount() > 0) {
  getView().findViewById(R.id.loadingPanel).setVisibility(View.GONE);
  getView().findViewById(android.R.id.list).setVisibility(View.VISIBLE);
}

, curstom Service, db , , .

0

costumCursorAdapter listView.

cursorLoader costumCursorAdapter . .

, Google - , ,  i.e. progress_bar.setVisibility(View.Gone);

, " " sulotion -

  • , int count = 0. reset 0 ( onClick ).
  • bindView , , count ++.
  • , 4. , :

            if (count==4){
            pb.setVisibility(View.GONE);
        }
    
  • , customAdapter 4- , , , .

  • 4 .

, -.

0

All Articles