문제

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

도움이 되었습니까?

해결책

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.

다른 팁

In addition to the right answer: it is a good idea in onLoadFinished() to have this check:

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

Otherwise, in case of relying on your curstomService to fill the db with data, the cursor may be empty and result would be no progress bar neither list with items showing up.

I have a costumCursorAdapter in a listView.

I also have a cursorLoader to swap the costumCursorAdapter when data is changed in the data base. DB data is entered by a user initiated search.

None of these advices worked, Google was no help - nothing stoped the progress bar once set to visible- i.e. progress_bar.setVisibility(View.Gone);

Eventually, as a simple and 'only for now' sulotion-

  1. I created a global variable to act as a counter int count = 0. It is reset to 0 with every search (in the onClick method of a button).
  2. In the bindView of the customAdapter, in the bottom of this code block- I initiated the count with count ++.
  3. At the top of the code block I check if the count reached 4. If it is, I hide the progress bar:

            if (count==4){
            pb.setVisibility(View.GONE);
        }
    
  4. This way, when the customAdapter binds the 4th view,I know it's resonable to stop the progress bar.

  5. I never have less than 4 items on the list.

Hope this helps someone.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top