繁体   English   中英

Android使用光标适配器在后台加载ListView

[英]Android load ListView in background using cursor adapter

我想使用背景线程使用游标适配器将数据加载到listview中,对谷歌搜索很多,但是找不到任何帮助材料。 我正在使用这种情况,以避免线程卡住。 它在主线程上工作正常,但我只需要在后台。 我已经实现了异步任务,但都没有用

class HeavyWorker extends AsyncTask < Cursor , Context , Void > {

    private ProgressDialog      progressDialog ;
    private Context             targetCtx ;

    public HeavyWorker ( Context context ) {
        this.targetCtx = context ;

        progressDialog = new ProgressDialog ( targetCtx ) ;
        progressDialog.setCancelable ( false ) ;
        progressDialog.setMessage ( "Retrieving data..." ) ;
        progressDialog.setTitle ( "Please wait" ) ;
        progressDialog.setIndeterminate ( true ) ;
    }

    @ Override
    protected void onPreExecute ( ) {
        progressDialog.show ( ) ;
    }

    @ Override
    protected Void doInBackground ( Cursor ... params ) {
        final ComplexGridViewColumnAdapter adapter = new ComplexGridViewColumnAdapter(context, cursor, true);
        Toast.makeText(context, "" + cursor.getCount() + " Records found", Toast.LENGTH_LONG).show();
        listView.setVisibility(VISIBLE);
        adapter.setListener(getContext(), NewComplexGridColumnView.this, currentQuestion.isHasHitoricalData(), currentQuestion.isSkuSelectionEnabled());
        listView.setAdapter(adapter);
       return null ;
    }

    @ Override
    protected void onPostExecute ( Void result ) {
        if(progressDialog != null && progressDialog.isShowing()){
            progressDialog.dismiss ( ) ;
        }
    }

这是我的异步任务类

您只需要使用Loaders 我不会附加代码示例,因为您可以自己找到它们,这很容易。 以下是一些链接:

在doInBackground()中更新UI元素不是一个好习惯。

您可以在doInBackground()中获得结果并调用侦听器方法。

像这样的东西:

 @ Override
protected Void doInBackground ( Cursor ... params ) {
    final ComplexGridViewColumnAdapter adapter = new ComplexGridViewColumnAdapter(context, cursor, true);
    Toast.makeText(context, "" + cursor.getCount() + " Records found", Toast.LENGTH_LONG).show();
    listener.updateListView(cursor);
   return null ;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM