[英]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.