ITKeyword,专注技术干货聚合推荐

注册 | 登录

解决android - Show Progress Indicator before fill listview using CursorLoader

itPublisher 分享于

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

推荐:Android Loader(三) 结合CursorLoader分析Loader相关源码

Android Loader(二) CursorLoader Android Loader(四) 自定义Loader从网络中获取文本数据 首先从加载数据的过程开始分析。 初始化Loader的方法是:getLoaderMana

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

android android-cursorloader android-loader
|
  this question
asked Apr 18 '13 at 16:34 elirigobeli 763 2 11 20

 | 

3 Answers
3

解决方法

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.


|
  this answer
answered Apr 18 '13 at 18:12 Joe Malin 7,604 1 13 16      Your answer helped me get this example . I believe this is the simplest way. Thank you –  elirigobeli Apr 19 '13 at 13:11

 | 

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.


|
  this answer
edited Aug 24 '14 at 21:17 fuesika 2,162 5 16 26 answered Aug 24 '14 at 20:49 Addicted 11 3      Showing loader infinitely will not be a good idea, instead show proper message "Record not found" when getCount()==0 –  Mohd Mufiz Oct 7 '14 at 9:36

 | 

I have a costumCursorAdapter in a listView.

推荐:Android联动ListView的实现

今天带来的是两列并排ListView关联滑动,这里面有两个知识点:1、两个ListView如何并列显示。2、如何关联滑动。 第一个问题,好像我之前的博客提到过,就是让Lis

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.


|
  this answer
answered Nov 30 '16 at 21:23 Tamir_B 1 1

 | 

推荐:深入源码解析Android中Loader、AsyncTaskLoader、CursorLoader、LoaderManager

如果对Loader、AsyncTaskLoader、CursorLoader、LoaderManager等概念不明白或不知道如何使用Loader机制,可参见博文Android中Loader及LoaderManager的使用(附源


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。